贫民窟里的程序高手

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

本文转载自: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;
        }         
    }

 文件上传II

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;
        }
    }

 

posted on 2016-09-28 15:00  贫民窟里的程序高手  阅读(1134)  评论(0编辑  收藏  举报