• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#webapi上传下载图片

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

客户端上传文件

string url = url + "webUploadFile";
            Uri server = new Uri(url);
            HttpClient httpClient = new HttpClient();

            MultipartFormDataContent multipartFormDataContent = new MultipartFormDataContent();

            StreamContent streamConent = new StreamContent(new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read));

            multipartFormDataContent.Add(streamConent, "jpg", fileName);

            HttpResponseMessage responseMessage = httpClient.PostAsync(server, multipartFormDataContent).Result;
            return responseMessage;

 

服务端接收文件


[Route("webUploadFile"), System.Web.Http.HttpPost]
public HttpResponseMessage webUploadFile()

{

 (HttpContext.Current.Request.Files.AllKeys.Any())
                {var httpPostedFiles = HttpContext.Current.Request.Files;
                    if (httpPostedFiles != null && httpPostedFiles.Count > 0)
                    {
                        // 获取文件
                        HttpPostedFile httpPostedFile = httpPostedFiles[0];
                        string fileExtension = ".jpg";// Path.GetExtension(httpPostedFile.FileName);// 文件扩展名
                        string fileId = "11";
                        string filePath = uploadPath + fileId + fileExtension;// 上传路径


                        httpPostedFile.SaveAs(filePath);

                        
                        string jsonres = "{\"code\":\"200\",\"message\":\"文件上传成功\", \"data\":{ \"fileUrl\":\"" + downloadurl+ "webDownloadFile?fileId=" + fileId + "\"}}";

                        return new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent(jsonres, System.Text.Encoding.UTF8, "application/json")};
                    }
                }
}

 

服务端下载文件

[Route("webDownloadFile"), System.Web.Http.HttpGet]   
        public HttpResponseMessage webDownloadFile()
        {
            if (HttpContext.Current.Request.Params.Count > 0 && HttpContext.Current.Request["fileId"] != null)
            {
                string fileId = HttpContext.Current.Request["fileId"];
                string fileName = fileId + ".jpg";

                string path = uploadPath + fileName;
                if (!File.Exists(path))
                {
                    return new HttpResponseMessage(HttpStatusCode.NotFound) { Content = new StringContent(Common.ReturnMessage("404", "文件不存在"), System.Text.Encoding.UTF8, "application/json") };
                }

                HttpResponseMessage result = null;

                FileStream fs = new FileStream(path, FileMode.Open);

                result = new HttpResponseMessage(HttpStatusCode.OK);
                result.Content = new StreamContent(fs);
                result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("image/jpeg");

                return result;
            }

            return new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(Common.ReturnMessage("400", "文件下载失败"), System.Text.Encoding.UTF8, "application/json") };
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap