ASP 指定下载文件的文件名

Response.ContentType = “application/octet-stream”
Response.AddHeader “Content-Disposition”, “attachment;
filename=test.csv”

ASP.NET实现下载地址隐藏和简单防盗链

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

从数据库中读取二进制数据,然后进行下载时,其文件名是当前的 ASP
页面文件名,使用以下代码,可以指定下载框中的文件名。

MSDN原文说明:

//流方式下载
protected void Button4_Click(object sender, EventArgs e)
{
string fileName = “aaa.txt”;//客户端保存的文件名
string filePath = Server.MapPath(“DownLoad/aaa.txt”);//路径

  • 如何将 Stream
    以中文名称下载?

附:此方法不能用于下载大型文件。

Response.ContentType = “application/x-zip-compressed”;
Response.AddHeader(“Content-Disposition”,
“attachment;filename=z.zip”);
澳门新葡萄京官网注册,string filename = Server.MapPath(“DownLoad/z.zip”);
Response.TransmitFile(filename);
}

相关阅读

//获取文件名

if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K
每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];

string filename = ((LinkButton)e.CommandSource).Text;

string fileName = “aaa.txt”;//客户端保存的文件名
string filePath = Server.MapPath(“DownLoad/aaa.txt”);//路径

此方法可以让下载地址隐藏,并且只能ie下载,一般的下载工具无法下载。

对大型文件使用此方法时,调用此方法可能导致异常。可以使用此方法的文件大小取决于
Web 服务器的硬件配置。有关更多信息,请参见位于 Microsoft
知识库上的文章
812406“PRB: Response.WriteFile Cannot Download a Large
File”(PRB:Response.WriteFile 无法下载大型文件)。

//WriteFile实现下载
protected void Button2_Click(object sender, EventArgs e)
{

//设置下载对话框显示的文件名,用Server.UrlEncode方法可以防止下载文件名出现乱码
Response.AddHeader(“Content-Disposition”, “attachment;filename=” +
Server.UrlEncode(file.Name));
Response.AddHeader(“Content-Length”, file.Length.ToString());
Response.ContentType = “application/octet-stream”;
Response.WriteFile(file.FullName);
Response.End();
}
else
{
lbErr.Text = “该文件不存在,请联系管理员!”;
}
}

string fileName =”asd.txt”;//客户端保存的文件名
string filePath=Server.MapPath(“DownLoad/aaa.txt”);//路径

if (filename != “”)
{

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

//得到文件地址
string path = Server.MapPath(“upload/”) + filename;
System.IO.FileInfo file = new System.IO.FileInfo(path);
if (file.Exists)
{//设置输出参数并输出文件流
Response.Clear();

}

Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.ContentType = “application/octet-stream”;
Response.AddHeader(“Content-Disposition”, “attachment; filename=” +
HttpUtility.UrlEncode(fileName));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0,
Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead – lengthRead;
}
Response.Close();
}
}

}
}

 

//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = “application/octet-stream”;
//通知浏览器下载文件而不是打开
Response.AddHeader(“Content-Disposition”, “attachment;  filename=” +
HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();

//TransmitFile实现下载
protected void Button1_Click(object sender, EventArgs e)
{

//WriteFile分块下载
protected void Button3_Click(object sender, EventArgs e)
{

FileInfo fileInfo = new FileInfo(filePath);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader(“Content-Disposition”, “attachment;filename=” +
fileName);
Response.AddHeader(“Content-Length”, fileInfo.Length.ToString());
Response.AddHeader(“Content-Transfer-Encoding”, “binary”);
Response.ContentType = “application/octet-stream”;
Response.ContentEncoding = System.Text.Encoding.GetEncoding(“gb2312”);
Response.WriteFile(fileInfo.FullName);
Response.Flush();
Response.End();
}

System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);

发表评论

电子邮件地址不会被公开。 必填项已用*标注