• 文件下载的几个问题


    其实就是直接写二进制流而已,不过有几点(越来越混乱……)

    /*
    1.ContentType
      如果要让浏览器直接打开某些文件(图片,文本,HTML),需要设置这个;
    2.Content-Disposition
      跟上面一个设置有联系,如果直接打开,设置inline,否则attachment
    3.Range

      如果要断点续传,需要用到这个(没有详细测试)

    4.文件名长度,中文不能超过18个,过多会有乱码

    5.文件名中的空格会被改成加号(+)

    */

    string name = Path.GetFileNameWithoutExtension(originalName);
    if (IsChinese(name) && name.Length > 15)
        name = name.Substring(0, 15);
    name = name + Path.GetExtension(originalName);
    name = System.Web.HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8);
    name = name.Replace("+", "%20");

    Response.AddHeader("Content-Range""bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());
    Response.ContentType 
    = contenttype;
    Response.AddHeader(
    "Content-Disposition", disposition + "; filename=" + name);

     

     另外,如果用ashx来做下载,如果用到Session(如验证权限等),需要实现IRequiresSessionState接口。

     参考:

    ASP:Response.ContentType 详细列表

    http://www.cnblogs.com/black263/archive/2008/03/05/1091053.html

    Content-Disposition的使用和注意事项

    http://www.qihangnet.com/PermaLink,guid,db65d50a-ba90-4229-a3a2-71b4f1b407b9.aspx

    ASP.NET利用HttpHandler实现多扩展名文件下载

    http://www.cnblogs.com/liping13599168/archive/2009/02/08/1386111.html

    Server.UrlEncode、HttpUtility.UrlDecode的区别

    http://blog.chinaunix.net/u1/46138/showart_1678968.html

    response.writefile文件名长度限制问题

    http://space.cnblogs.com/question/2967/

    完全成为了一个"google程序员",所有东西都是在google找到来拼的……

  • 相关阅读:
    CF149D Coloring Brackets
    CF508D
    CF483C Diverse Permutation
    【纪念】我写过几乎最长的代码
    .net core图片上传详解
    layui插件croppers的使用
    关于日常操作中sql的性能
    leeCode 278
    leeCode刷题 1078
    leeCode刷题 lc184
  • 原文地址:https://www.cnblogs.com/nonlyli/p/1387486.html
Copyright © 2020-2023  润新知