c# API接受图片文件以文件格式上传图片

2018-09-10 01:14:33来源:博客园 阅读 ()

新老客户大回馈,云服务器低至5折

   /// 文件图片上传 
        /// </summary> 
        /// <returns>成功上传返回上传后的文件名</returns>
        [HttpPost]
        public async Task<IHttpActionResult> UpLoadImageFiles()
        {

            var files = HttpContext.Current.Request.Files;
            if (files.AllKeys.Any())
            {
                using (HttpClient client = new HttpClient())
                {
                    HttpContextBase HttpContext = (HttpContextBase)Request.Properties["MS_HttpContext"];
                    var request = new HttpRequestMessage(HttpMethod.Post, ConfigurationManager.AppSettings["SaveToImgaes"].ToString() + "/Upload/");
                    var content = new MultipartFormDataContent();
                    content.Add(new StreamContent(HttpContext.Request.Files[0].InputStream), "file", "file.jpg");
                    request.Content = content;
                    var response = await client.SendAsync(request);
                    response.EnsureSuccessStatusCode();
                    var filenamestr = await response.Content.ReadAsStringAsync();//结果
                }
            }
            return Ok();
        }

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:ie下警告console未定义

下一篇:记录.net使用ueditor富文本编辑器