asp.net 文件下载实现代码
程序员文章站
2024-03-11 20:31:49
复制代码 代码如下:/// /// 文件下载 /// ///
/// <summary>
/// 文件下载
/// </summary>
/// <param name="savename">文件名</param>
/// <param name="fullfilename">文件全名</param>
/// <param name="response">response</param>
public static void savefile(string savename,string fullfilename,system.web.httpresponse response)
{
try
{
fileinfo downloadfile = new fileinfo(fullfilename);
if (downloadfile.exists)
{
response.clear();
response.clearheaders();
response.buffer = false;
response.contenttype = "application/octet-stream";
response.appendheader("content-disposition", "attachment;filename="+ system.web.httputility.urlencode(savename,system.text.encoding.utf8));
response.appendheader("content-length", downloadfile.length.tostring());
response.writefile(downloadfile.fullname);
response.flush();
response.end();
}
else
{
//文件不存在
}
}
catch
{
//文件不存在
}
}
复制代码 代码如下:
/// <summary>
/// 文件下载
/// </summary>
/// <param name="savename">文件名</param>
/// <param name="fullfilename">文件全名</param>
/// <param name="response">response</param>
public static void savefile(string savename,string fullfilename,system.web.httpresponse response)
{
try
{
fileinfo downloadfile = new fileinfo(fullfilename);
if (downloadfile.exists)
{
response.clear();
response.clearheaders();
response.buffer = false;
response.contenttype = "application/octet-stream";
response.appendheader("content-disposition", "attachment;filename="+ system.web.httputility.urlencode(savename,system.text.encoding.utf8));
response.appendheader("content-length", downloadfile.length.tostring());
response.writefile(downloadfile.fullname);
response.flush();
response.end();
}
else
{
//文件不存在
}
}
catch
{
//文件不存在
}
}