• 下载网络文件,网络流转换为内存流


    string fileext = CSA_BLL.Common.Utils.GetFileExt(filePath);
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(filePath);
    request.Method = "GET";
    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
    {
    if (response.StatusCode == HttpStatusCode.OK)
    {
    Stream rs = response.GetResponseStream();
    //网络流转换为内存流
    var ms = StreamToMemoryStream(rs);
    ms.Seek(0, SeekOrigin.Begin); int buffsize = (int)ms.Length; //rs.Length 此流不支持查找,先转为MemoryStream
    byte[] bytes = new byte[buffsize];

    ms.Read(bytes, 0, buffsize);
    ms.Flush(); ms.Close();
    rs.Flush(); rs.Close();
    //以文件流的方式下载
    Response.Charset = "utf-8";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
    Response.ContentType = "application/octet-stream";
    if (HttpContext.Request.UserAgent.ToUpper().Contains("MSIE") ||
    HttpContext.Request.UserAgent.ToUpper().Contains("TRIDENT") ||
    HttpContext.Request.UserAgent.ToUpper().Contains("EDGE"))
    {
    fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
    }
    else
    {
    fileName = fileName.Replace(" ", "_");
    }
    //fileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
    Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName + "." + fileext));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();

    }
    }
    return new EmptyResult();
    }

    MemoryStream StreamToMemoryStream(Stream instream)
    {
    MemoryStream outstream = new MemoryStream();
    const int bufferLen = 4096;
    byte[] buffer = new byte[bufferLen];
    int count = 0;
    while ((count = instream.Read(buffer, 0, bufferLen)) > 0)
    {
    outstream.Write(buffer, 0, count);
    }
    return outstream;
    }

  • 相关阅读:
    对日期和时间的处理 NSCalendar
    常用的预处理器指令
    NSString常用方法
    UINavigationBar导航栏相关设置
    Xcode和github入门详细教程
    IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
    ios设备 分辨率(转)
    iOS本地推送
    cunix的opencv学习经验
    mpeg压缩输入格式---打包模式和平面模式
  • 原文地址:https://www.cnblogs.com/roper/p/11804190.html
Copyright © 2020-2023  润新知