码迷,mamicode.com
首页 > Windows程序 > 详细

(转)WebApi 上传文件

时间:2014-11-13 12:23:52      阅读:859      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   ar   os   sp   for   

本文转载自:http://www.cnblogs.com/zj1111184556/p/3494502.html

public class FileUploadController : ApiController
    {

        public async Task<HttpResponseMessage> Post()
        {
            // 检查是否是 multipart/form-data
            if (!Request.Content.IsMimeMultipartContent("form-data"))
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            HttpResponseMessage response = null;

            try
            {
                // 设置上传目录
                var provider = new MultipartFormDataStreamProvider(@"F:\\StudyProject\\webapi2demo\\CSdemo\\UpLoad");
                // 接收数据,并保存文件
                var bodyparts = await Request.Content.ReadAsMultipartAsync(provider);
                response = Request.CreateResponse(HttpStatusCode.Accepted);
            }
            catch 
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest); 
            }
            return response;
        }         
    }
public class FileUploadController : ApiController
    {
        public string Post() 
        {

            HttpPostedFile file = HttpContext.Current.Request.Files[0];
            string strPath = "D:\\MyProjects\\StudySolution\\RestDemo\\Upload\\test2.rar" ;
            file.SaveAs(strPath);
            string result = "0";
      

            return result;
        }
    }

 

(转)WebApi 上传文件

标签:style   blog   http   io   color   ar   os   sp   for   

原文地址:http://www.cnblogs.com/wpcnblog/p/4094253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!