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