asp.net Web Api 上传文件

  一、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;

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zyyjxg.html