有的时候我们不想让用户直接在IE中打开已知类型的文件,比如Word,而希望能直接下载,这时候可用下面代码来替换Response.Redirect
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;FileName="+YourFileName);
Response.BinaryWrite((byte[])YourFileData.Rows[0]["AttachmentContent"]);
Response.End();
打印 | 张贴于 2004-05-25 15:00:00 | Tag:程序人生
留言反馈
輸出成下載檔案寫法
Response.Buffer=true;Response.Clear();Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition",...
http://dev.wl668.com/net/ASP/20051135202_4199288.shtml
没有一个看明白的
这个需要是绝对地址,如果我发布后,这样用就不可以了吧,怎么改成相对地址啊?
Dim FileName As String = "D:\wwwroot\classfile\1000000019.doc"
Dim fileStream As New FileStream(FileName, FileMode.Open)
Dim fileSize As Long = fileStream.Length
Dim inta As Integer = CInt(fileSize)
Context.Response.ContentType = "application/octet-stream"
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" & HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8))
Context.Response.AddHeader("Content-Length", fileSize.ToString())
Dim fileBuffer(inta) As Byte
fileStream.Read(fileBuffer, 0, inta)
fileStream.Close()
Context.Response.BinaryWrite(fileBuffer)
Context.Response.End()
End Sub
{
try
{
string str = "<html>";
for(int page=0; page<4; page++)
{
str += "<TABLE borderColor='black' cellSpacing='0' borderColorDark='white' cellPadding='3' border='1'>";
str += "<tr><th>描述</th></tr>";
for(int i=1; i< 10; i++)
{
str =str + "<tr><td>测试" + i.ToString() + "</td></tr>";
}
str +="</table>";
//.doc 换页
str +="<br clear=all style='mso-special-character:line-break;page-break-before:always'>";
}
str += "</html>";
byte[] buff = System.Text.Encoding.Unicode.GetBytes(str);
//byte[] buff = System.Text.Encoding.UTF8.GetBytes(str);
byte[] outBuff = new byte[buff.Length + 2];
// 使用文件流方式写入UniCode编码的doc文件。
byte[] mark = {0xFF,0xFE} ;
outBuff[0] = mark[0];
outBuff[1] = mark[1];
for(int i=0; i< buff.Length; i++)
{
outBuff[i+2] = buff[i];
}
Context.Response.ContentType="application/octet-stream";
string fileName = "测试.doc";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + "\"");
Context.Response.AddHeader("Content-Length",outBuff.Length.ToString());
Response.BufferOutput = true;
Response.Clear();
Context.Response.BinaryWrite(outBuff);
Context.Response.End();
}
catch(Exception ex)
{
ex.ToString();
}
finally
{
}
}
用Response.Redirect("test.xls"),还是提示下载啊!
long fileSize = fileStream.Length;
Context.Response.ContentType="application/octet-stream";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8) + "\"");
Context.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
fileStream.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
请教高手指点
各位高人指点下吧!
那個方法我用過,但是放到Excel中都是亂碼。怎麼辦?
long fileSize = fileStream.Length;
Context.Response.ContentType="application/octet-stream";
Context.Response.AddHeader("Content-Disposition","attachment; filename=\"" + fileName + "\";");
Context.Response.AddHeader("Content-Length",fileSize.ToString());
byte[] fileBuffer=new byte[fileSize];
fileStream.Read(fileBuffer, 0, (int)fileSize);
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
你的那个偶数个汉字的问题解决了没有?
最近我也遇到了这个问题,希望大家帮忙!
谢谢
厦门松声宾馆由厦门扬帆电脑公司做的
有用到ASP
分机新新 .xls
分机新新+.xls
分机+新新.xls
用上面三个文件名下载失败。
分+机新新.xls
分机新+新.xls
用上面二个文件名下载成功。
String filename = Request.Params["filename"];
//中文文件名需要编码
Response.AddHeader( "content-disposition","attachment; filename="+HttpUtility.UrlEncode(filename,Encoding.UTF8 ) );
另一台机是win2kas + ie5,下载.doc文件时却时在ie中打开的
[C#]
public static string UrlEncode(
string str
);
Page.Response.AddHeader("Content-Disposition","attachment; filename=" + HttpUtility.UrlEncode(fileName,Encoding.UTF8 ) );
can't you get the fileid from the filename ?