(轉)WebApi 上傳文件


本文轉載自: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;
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM