引用一下js:fileupload.js
function uploadImg1() {
var f = document.getElementById('uploadFile1').files[0]
var src = window.URL.createObjectURL(f);
document.getElementById('uploadPreview1').src = src;
//获取图片信息
var f = document.getElementById('uploadFile1').files[0];
if (!!f) {
learun.loading(true, '正在保存...');
$.ajaxFileUpload({
url: top.$.rootUrl + "/JSSS_imageUpload/JSSS_imageUpload/UploadFile",
secureuri: false,
fileElementId: 'uploadFile1',
dataType: 'json',
success: function (data) {
console.log(data)
$("#F_Photo1").val(data.data);
learun.loading(false);
if (data.code == 200) {
learun.alert.success('保存成功');
}
}
});
}
};
/// <summary> /// 上传头像 /// </summary> /// <returns></returns> [HttpPost] public ActionResult UploadFile() { HttpFileCollection files = System.Web.HttpContext.Current.Request.Files; string ImageName = files[0].FileName; //没有文件上传,直接返回 if (files[0].ContentLength == 0 || string.IsNullOrEmpty(files[0].FileName)) { return HttpNotFound(); } string dir = DateTime.Now.ToString("yyyyMMdd"); if (!Directory.Exists(System.AppDomain.CurrentDomain.BaseDirectory + $@"Resource\{dir}")) { Directory.CreateDirectory(System.AppDomain.CurrentDomain.BaseDirectory + $@"Resource\{dir}"); } string path = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + $@"Resource\{dir}\" + ImageName; string imagennn = $@"\Resource\{dir}\" + ImageName; files[0].SaveAs(path); return JsonResult(imagennn); }