一、WebApi上使用MultipartFormDataStreamProvider来接收文件时,会自动将文件保存至指定目录下,文件名什么的都是自动生成的。代码如下:
[HttpPost, Route("savefiles")] public async Task<bool> SaveFile(string projectId, string eventId) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } string root = Path.Combine(HostingEnvironment.MapPath(ConfigurationManager.AppSettings["FileStorePath"]), DateTime.Now.ToShortDateString(), projectId); if (!Directory.Exists(root)) Directory.CreateDirectory(root); var provider = new MultipartFormDataStreamProvider(root); try { await Request.Content.ReadAsMultipartAsync(provider); } catch (Exception ex) { WebConfig.LoggerInstance.Error("上传文件出现异常:" + ex.Message); } return true;