上传图片

<input id="fil" type="file" name="file" />
<input id="Hidden1" type="hidden" name="lujign" />
<img src="" style="200px;height:200px;" id="ima" />

me en 


$("#fileUpLoad").change(function () { //单文件长传所以默认取第一个 //FormData var formData = new FormData($("#formData")[0]); $.ajax({ url: '/Product/UploadFile', type: 'post', //不处理数据 processData: false, //不处理数据类型 contentType: false, //没有缓存 cache: false, //数据是表单数据 data: formData, success: function (data) { $("input[name=FileName]").val(data); $("#showImage").attr("src",data); } ///控制器 public string UploadFile() { //获取文件 HttpPostedFileBase httpFile=Request.Files[0]; if (httpFile != null) { //目录的单词是Dirctory 字典Dinctionary //获取路径 string strPath = Server.MapPath("~/images/"); //判断目录是否存在 if (!Directory.Exists(strPath)) //如果目录不存在进行创建目录 Directory.CreateDirectory(strPath); //string newPath=strPath+"/"+httpFile.FileName; //获取的是全路径 string newPath = Path.Combine(strPath,httpFile.FileName); httpFile.SaveAs(newPath); return "/images/"+httpFile.FileName; } return null;
}
原文地址:https://www.cnblogs.com/caoxiaole/p/9371880.html