zoukankan      html  css  js  c++  java
  • netcore 从api下载文件并直接返回

    [HttpGet]
            public IActionResult HttpReturnFile()
            {
                string url = "http://localhost:99/api/HttpFile/GetFile";
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "GET";
                using (WebResponse webRes = request.GetResponse())
                {
                    int length = (int)webRes.ContentLength;
                    HttpWebResponse response = webRes as HttpWebResponse;
                    Stream stream = response.GetResponseStream();
                    var contentdisposition = response.Headers["Content-Disposition"];
                    var filename = Util.Midstr(contentdisposition, "filename=", ";");
                    var ext = "."+filename.Split('.')[1];
                    new FileExtensionContentTypeProvider().Mappings.TryGetValue(ext, out var contenttype);
                    //读取到内存
                    MemoryStream stmMemory = new MemoryStream();
                    byte[] buffer = new byte[length];
                    int i;
                    //将字节逐个放入到Byte中
                    while ((i = stream.Read(buffer, 0, buffer.Length)) > 0)
                    {
                        stmMemory.Write(buffer, 0, i);
                    }
                    stmMemory.Close();
                    return File(buffer, contenttype ?? "application/octet-stream",filename);
                }
            }

     url下载文件地址

  • 相关阅读:
    Java 抽象类
    Java final 关键字
    Java 异常机制
    hashcode和equals
    DevExpress 柱状图
    Windows X64平台搭建Java开发环境
    J2EE 学习路线
    winform 客户端采用HTTP协议与服务端通信
    C# 处理Json
    性能分析工具 DotTrance
  • 原文地址:https://www.cnblogs.com/huanyun/p/11350840.html
Copyright © 2011-2022 走看看