Asp.Net Mvc 带参数上传图片
1两个JS文件
2.前端页面
<input type="file" name="inp_Photo" id="inp_Photo" />
3.JS
$.ajaxFileUpload({ url: '/Home/UploadUserPhoto', type: 'post', secureuri: false, fileElementId: 'inp_Photo', // 上传文件的id、name属性名 dataType: 'text/html', //返回值类型,一般设置为json、application/json success: function (name) { $.post("/Home/AddUser",{imgName:name},function(){ imgName:JSON.parse(name)}); }, error: function (data, status, e) { alert(e); } });
4.后台代码
public ActionResult UploadUserPhoto() { HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files; if (hfc.Count == 1) { string path = "/OfficerFiles/"; string uploadPath = Server.MapPath(path); if (!Directory.Exists(uploadPath)) { Directory.CreateDirectory(uploadPath); } string fileName = DateTime.Now.ToString("yyyyMMddHHmmssms") + ".jpg"; uploadPath += fileName; hfc[0].SaveAs(uploadPath); return Json(fileName,text/html);
}
}
注: 上传图片参数
$.ajaxFileUpload 方法中: data:{Id:"123"}
后台获取方式: NameValueCollection nvc = System.WebHttpContext.Current.Request.Form; nvc.Get("Id");