zoukankan      html  css  js  c++  java
  • C# webapi 上传下载图片

    客户端上传文件

    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()

    {

      if (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") };
            }
  • 相关阅读:
    走进JavaWeb技术世界8:浅析Tomcat9请求处理流程与启动部署过程
    走进JavaWeb技术世界7:Tomcat和其他WEB容器的区别
    走进JavaWeb技术世界6:Tomcat5总体架构剖析
    走进JavaWeb技术世界5:初探Tomcat的HTTP请求过程
    走进JavaWeb技术世界4:Servlet 工作原理详解
    走进JavaWeb技术世界3:JDBC的进化与连接池技术
    [转]115个Java面试题和答案——终极列表(下)
    [转]115个Java面试题和答案——终极列表(上)
    [转]Spring MVC 4常用的那些注解
    [转]spring4.x注解概述
  • 原文地址:https://www.cnblogs.com/jhlong/p/10784383.html
Copyright © 2011-2022 走看看