asp.net|下载
搞了好久也没有搞通,网上有很多事例都是一样的:IE支持的文件就会直接打开,而其它的像ZIP之类的文件不识别,就会弹出下载或打开的对话框。
突然一想是不是有什么文件筛选的问题,果然在写文件之前将Filter清除就可以像附件一样使用了。
VB.net:
Dim filename As String = "a.txt"
If filename <> "" Then
Dim path As String = Server.MapPath(filename)
Dim file As System.IO.FileInfo = New System.IO.FileInfo(path)
If file.Exists Then
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name)
Response.AddHeader("Content-Length", file.Length.ToString())
Response.ContentType = "application/octet-stream"
Response.Filter.Close()
Response.WriteFile(file.FullName)
Response.End()
Else
Response.Write("This file does not exist.")
End If
End If
----------------------------------------------------------------
c#:
string filename = "a.txt";
if (filename != "")
{
string path = Server.MapPath(filename);
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
else
{
Response.Write("This file does not exist.");
}
}