asp.net webapi 上传图片

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

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

谢谢

posted @ 2016-07-20 15:03  游子善心  阅读(7)  评论(0编辑  收藏  举报  来源