最近因为项目需要! 图片上传 功能
查了很多资料,了解到 <input type="file" style="width: 250px; "> 图片只能进行form提交上传保存! 不能使用ajax 进行保存。图片上传后的预览效果! 没找到合适的解决办法
俺们只能用比较保守的方法使用 上传在本地后在进行显示!
后台进行保存上传的图片 代码如下
HttpPostedFileBase uploadFile= Request.Files[0]; //得到文件
string fileName = uploadFile.FileName;
string KZ = Path.GetExtension(fileName).ToLower();
string path = "";
if (!(KZ == ".png" || KZ == ".gif" || KZ == ".jpg" || KZ == ".jpeg"))
{
return Json("图片文件格式错误!", JsonRequestBehavior.AllowGet);
}
else
{
Random r = new Random();
LogoFileName = DateTime.Now.ToString("yyyyMMddhhmmss") + r.Next(100000, 999999) + "Logo" + KZ;
string directoryPath = Server.MapPath("~/Content/File/Images/");
if (!Directory.Exists(directoryPath))//不存在这个文件夹就创建这个文件夹
{
Directory.CreateDirectory(Server.MapPath("~/Content/File/Images/"));
}
path = Server.MapPath("~/Content/File/Images/") + LogoFileName;
uploadFile.SaveAs(path);
}