public async Task <AjaxData> UploadFileAsync(string parameter = "") { AjaxData <IEnumerable <string> > ajaxData = AjaxData.GetAjaxData <IEnumerable <string> >(); try { var files = HttpContext.Current.Request.Files; return(await UploadFileMethodAsync(files, parameter)); } catch (Exception ex) { ajaxData.Msg = $"{ex.Message}-{ex.StackTrace}"; } return(ajaxData); }
private async Task <AjaxData> UploadFileMethodAsync(HttpFileCollection files, string msg) { AjaxData <IEnumerable <string> > ajaxData = AjaxData.GetAjaxData <IEnumerable <string> >(); try { var savepaths = new List <string>();//保存的文件名集合 if (files != null && files.Count > 0) { var physicdir = HttpContext.Current.Server.MapPath(string.Format("~/{0}", TempDir)); if (!System.IO.Directory.Exists(physicdir)) { Directory.CreateDirectory(physicdir); } foreach (var key0 in files.AllKeys) { if (string.IsNullOrWhiteSpace(key0)) { continue; } var itemfile = files[key0]; var filename = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), Path.GetExtension(itemfile.FileName)); //文件名 var virpath = string.Format("/{0}/{1}", TempDir, filename); //保存的文件虚拟目录 var physicpath = Path.Combine(physicdir, filename); //保存的文件物理路径 using (var stream = System.IO.File.Open(physicpath, FileMode.Create)) { await itemfile.InputStream.CopyToAsync(stream); } savepaths.Add(virpath); } ajaxData.Res = savepaths; ajaxData.OK = true; ajaxData.Msg = msg; } else { ajaxData.Msg = "没有接收到文件"; } } catch (Exception ex) { ajaxData.Msg = $"{ex.Message}-{ex.StackTrace}"; } return(ajaxData); }