C# WebApi 上传文件

来源:互联网 发布:python 爬虫多进程 编辑:程序博客网 时间:2024/06/05 07:14
public class FileUploadController : ApiController 2     { 3  4         public async Task<HttpResponseMessage> Post() 5         { 6             // 检查是否是 multipart/form-data 7             if (!Request.Content.IsMimeMultipartContent("form-data")) 8                 throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); 9             HttpResponseMessage response = null;10 11             try12             {13                 // 设置上传目录14                 var provider = new MultipartFormDataStreamProvider(@"F:\\StudyProject\\webapi2demo\\CSdemo\\UpLoad");15                 // 接收数据,并保存文件16                 var bodyparts = await Request.Content.ReadAsMultipartAsync(provider);17                 response = Request.CreateResponse(HttpStatusCode.Accepted);18             }19             catch 20             {21                 throw new HttpResponseException(HttpStatusCode.BadRequest); 22             }23             return response;24         }         25     }
复制代码

 文件上传II

复制代码
 1 public class FileUploadController : ApiController 2     { 3         public string Post()  4         { 5  6             HttpPostedFile file = HttpContext.Current.Request.Files[0]; 7             string strPath = "D:\\MyProjects\\StudySolution\\RestDemo\\Upload\\test2.rar" ; 8             file.SaveAs(strPath); 9             string result = "0";10       11 12             return result;13         }14     }
复制代码
原创粉丝点击