public void InsertFile(marketFilesEntity ent) { fileRepository.Insert(ent); }
public async Task <fileViewModel> PostFile(string ext) { try { // 是否请求包含multipart/form-data。 if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = HttpContext.Current.Server.MapPath("/UploadFiles/"); if (!Directory.Exists(HttpContext.Current.Server.MapPath("~/UploadFiles/"))) { Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/UploadFiles/")); } var provider = new MultipartFormDataStreamProvider(root); StringBuilder sb = new StringBuilder(); // Holds the response body // 阅读表格数据并返回一个异步任务. await Request.Content.ReadAsMultipartAsync(provider); // 如何上传文件到文件名. foreach (var file in provider.FileData) { string orfilename = file.Headers.ContentDisposition.FileName.TrimStart('"').TrimEnd('"'); FileInfo fileinfo = new FileInfo(file.LocalFileName); //sb.Append(string.Format("Uploaded file: {0} ({1} bytes)\n", fileInfo.Name, fileInfo.Length)); //最大文件大小 //int maxSize = Convert.ToInt32(SettingConfig.MaxSize); marketreportInfoApp app = new marketreportInfoApp(); marketFilesEntity ent = new marketFilesEntity() { id = System.Guid.NewGuid().ToString(), ContextType = "image/png", }; app.InsertFile(ent); using (new Impersonator("administrator", "", "!QAZ2wsx")) { if (Directory.Exists(System.Configuration.ConfigurationManager.AppSettings["filePatch"] + "\\" + System.DateTime.Now.ToString("yyyyMM")) == false)//如果不存在就创建file文件夹 { Directory.CreateDirectory(System.Configuration.ConfigurationManager.AppSettings["filePatch"] + "\\" + System.DateTime.Now.ToString("yyyyMM")); } fileinfo.CopyTo(System.Configuration.ConfigurationManager.AppSettings["filePatch"] + "\\" + System.DateTime.Now.ToString("yyyyMM") + "\\" + ent.id + "." + ext, true); // System.IO.File.Copy(fileDir + "\\" + ent.id + ".jpg", System.Configuration.ConfigurationManager.AppSettings["filePatch"] + "\\" + System.DateTime.Now.ToString("yyyyMM") + "\\" + ent.id + ".jpg", true); } fileinfo.Delete();//删除原文件 return(new fileViewModel() { fileId = ent.id }); // } } catch (System.Exception e) { return(new fileViewModel() { fileId = e.Message }); } return(new fileViewModel() { fileId = "" }); }