webapi文件上传
webapi文件上传
- public async Task<JsonResult<ResultInfo>> PublishingNews([SwaggerFileUpload] string title, string content)
- {
- FileInfo fileinfo = null;
- //是否上传了图片
- if (Request.Content.IsMimeMultipartContent())
- {
- //这里路径是服务器虚拟路劲,也就是文件要保存的地方
- var provider = new ExtensionMultipartFormDataStreamProvider(GetCurrentFilePaht(VirtualPath));
- try
- {
- //异步保存文件
- await Request.Content.ReadAsMultipartAsync(provider);
- //遍历多文件获取文件信息
- foreach (MultipartFileData file in provider.FileData)
- {
- fileinfo = new FileInfo(file.LocalFileName);
- //过滤文件类型
- string fileFilt = ".gif|.jpg|.php|.jsp|.jpeg|.png|......";
- if (string.IsNullOrEmpty(fileinfo.Extension) || fileFilt.IndexOf(fileinfo.Extension) <= -1)
- {
- //删除文件
- fileinfo.Delete();
- return Json(ResultOperationFailed("只能上传图片", "图片上传失败!"));
- }
- }
-
- }
- catch (System.Exception e)
- {
- return Json(ResultOperationFailed(e.Message,"图片上传失败!"));
- }
- }
只要心中有梦,不管什么天气都适合睡觉