首页 新闻 会员 周边 捐助

二进制流文件下载

1
悬赏园豆:10 [已解决问题] 解决于 2013-03-13 14:41

我的上传文件是一二进制流存在数据库中的,现在我要做下载,点下载的时候谈框框改怎么写的啊。我这样写 图片文件就不谈框框直接在线看了:

string fileName = AnnexModel.ANNEXNAME;//防止文件名乱码
Response.Clear();
Response.AppendHeader(
"Content-disposition:", "inline; filename=" + fileName);
Response.ContentType
= AnnexModel.TYPE;
Response.ContentType
= "application/octet-stream";
Response.OutputStream.Write(AnnexModel.ANNEX,
0, AnnexModel.ANNEXSIZE);
Response.End();
问题补充:

如何做成一点下载,不论什么类型的文件都弹出下载框呢?

小七灬的主页 小七灬 | 初学一级 | 园豆:102
提问于:2011-07-20 13:04
< >
分享
最佳答案
0

1. 下载文件:

 Response.AppendHeader("Content-disposition:", "attachment; filename=" + fileName);

 

2. 解决乱码

可以使用下面这个类(从 ASP.NET MVC3 中提取并修改的):

public static class TransmitFileHelper
{
public static string CreateRfc2231HeaderValue(string filename)
{
StringBuilder builder
= new StringBuilder("attachment; filename*=UTF-8''");
foreach (byte num in Encoding.UTF8.GetBytes(filename))
{
if (IsByteValidHeaderValueCharacter(num))
builder.Append((
char)num);
else
AddByteToStringBuilder(num, builder);
}
return builder.ToString();
}
private static void AddByteToStringBuilder(byte b, StringBuilder builder)
{
builder.Append(
'%');
int num = b;
AddHexDigitToStringBuilder(num
>> 4, builder);
AddHexDigitToStringBuilder(num
% 0x10, builder);
}
private static void AddHexDigitToStringBuilder(int digit, StringBuilder builder)
{
builder.Append(
"0123456789ABCDEF"[digit]);
}
private static bool IsByteValidHeaderValueCharacter(byte b)
{
if ((0x30 <= b) && (b <= 0x39)) return true;
if ((0x61 <= b) && (b <= 0x7a)) return true;
if ((0x41 <= b) && (b <= 90)) return true;
switch (b)
{
case 0x3a:
case 0x5f:
case 0x7e:
case 0x24:
case 0x26:
case 0x21:
case 0x2b:
case 0x2d:
case 0x2e:
return true;
}
return false;
}
}

调用示例:

var v = TransmitFileHelper.CreateRfc2231HeaderValue("测试.pdf");
//attachment; filename*=UTF-8''%E6%B5%8B%E8%AF%95.pdf
收获园豆:10
鹤冲天 | 老鸟四级 |园豆:2379 | 2011-07-20 13:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册