您的位置:首页 > 其它

文件接收上传

2015-08-06 13:52 267 查看
protected void Page_Load(object sender, EventArgs e)
{

HttpPostedFile jpeg_image_upload = Request.Files["Filedata"];

string imageId =DateTime.Now.Ticks.ToString();
string fileName = Path.GetFileName(jpeg_image_upload.FileName);
string fileExtension = Path.GetExtension(fileName);
string uploadName = imageId + fileExtension;

if (jpeg_image_upload.ContentLength > 0)
{

string imageUrl = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/" + uploadName);
if (!Directory.Exists(Path.GetDirectoryName(imageUrl)))
{
Directory.CreateDirectory(Path.GetDirectoryName(imageUrl));
}
jpeg_image_upload.SaveAs(imageUrl);

//保存大图并水印
string bigimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/c" + uploadName);
if (!Directory.Exists(Path.GetDirectoryName(bigimg)))
{
Directory.CreateDirectory(Path.GetDirectoryName(bigimg));
}
NCurrtImg.GreateMiniImage(imageUrl, bigimg, 600, 600, "COM");//生成缩略图
//CurrtImg.addWaterMark(tempimg, bigimg, "I");//水印

//保存中图
string midimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName);
if (!Directory.Exists(Path.GetDirectoryName(midimg)))
{
Directory.CreateDirectory(Path.GetDirectoryName(midimg));
}
NCurrtImg.GreateMiniImage(imageUrl, midimg, 200, 200, "COM");//生成缩略图

//保存小图
string smallimg = Server.MapPath("/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/s" + uploadName);
if (!Directory.Exists(Path.GetDirectoryName(smallimg)))
{
Directory.CreateDirectory(Path.GetDirectoryName(smallimg));
}
NCurrtImg.GreateMiniImage(imageUrl, smallimg, 33, 33, "COM");//生成缩略图

Response.StatusCode = 200;
string returnStr = jpeg_image_upload.ContentLength.ToString();//0 size
returnStr = returnStr + "|" + "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM") + "/m" + uploadName;//1 url
returnStr = returnStr + "|" + fileName; //2 title
returnStr = returnStr + "|" + imageId; //3 id
returnStr = returnStr + "|" + "wokaokao"; //4 name
Response.Write(returnStr);
Response.End();
}
}


public void UpImg(JTZK_ProductModel Model)
{
HttpPostedFile file = Request.Files["PicUrl"];
if (file == null)
{
// return ...
}
else
{
string fileName = System.IO.Path.GetFileName(file.FileName);
string fileExtension = System.IO.Path.GetExtension(fileName);
string uploadName = Guid.NewGuid() + fileExtension;
string dir = "/UpLoadPic/Product/" + DateTime.Now.ToString("yyyyMM");

string imgpath = dir + "/c" + uploadName;
Model.PicUrl = imgpath;
System.IO.Directory.CreateDirectory(Server.MapPath(dir));//创建文件夹.
imgpath = Server.MapPath(imgpath);
file.SaveAs(imgpath);

string smallimg = "";
string midimg = "";
smallimg = Server.MapPath(dir + "/s" + uploadName);
NCurrtImg.GreateMiniImage(imgpath, smallimg, 100, 100, "Cut");

midimg = Server.MapPath(dir + "/m" + uploadName);
NCurrtImg.GreateMiniImage(imgpath, midimg, 300, 300, "Cut");
}

}


如果失败 考虑权限问题 和路径问题 在 saveAs前一定要创建路径

#region 文件上传
public ActionResult FileUpload()
{
HttpPostedFileBase postFile = Request.Files["fileUp"];//接收文件数据
if (postFile == null)
{
return Content("no:请选择上传文件");
}
else
{
string fileName = Path.GetFileName(postFile.FileName);//文件名称
string fileExt = Path.GetExtension(fileName);//文件的扩展名称.
if (fileExt == ".jpg")
{
string dir = "/ImagePath/" + DateTime.Now.Year + "/" + DateTime.Now.Month + "/" + DateTime.Now.Day + "/";
Directory.CreateDirectory(Path.GetDirectoryName(Request.MapPath(dir)));//创建文件夹.
string newfileName = Guid.NewGuid().ToString();//新的文件名称.
string fullDir = dir + newfileName + fileExt;//完整的路径.
postFile.SaveAs(Request.MapPath(fullDir));//保存文件.
return Content("ok:" + fullDir);

}
else
{
return Content("no:文件格式错误!!");
}

}
}
#endregion
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: