文件下载的几个问题

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

/*
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找到来拼的……

 

 

posted @ 2009-02-10 15:16  果果’er  阅读(319)  评论(0编辑  收藏  举报