C# 文件下载时改变文件名
来源:互联网 发布:线切割自动编程软件 编辑:程序博客网 时间:2024/06/05 07:23
在项目中有时会出现下载文件的问题,服务器上保存的文件也许是已GUID命名的文件,但在下载时,也许需要指定一个文件名。我项目中就用到了,到这里做下笔记:
首先在页面上放一个<iframe>标签:
<iframe id="ifrLoad" frameborder="0" name="weidu" scrolling="no" width="100%" height="100%">
</iframe>
再在js中写如下方法:
function FileDownLoad(filePath, fileName) {
document.getElementById("ifrLoad").setAttribute("src", encodeURI("FileDownLoad.aspx?filePath=" + filePath + "&fileName=" + fileName));
}
注意文件名如果是中文要记得用:js 的encodeURI()方法进行编码传输
再到FileDownLoad.aspx页面后台代码进行转码操作,直接贴代码:
public partial class FileDownLoad : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.Params["filePath"];
string fileName = HttpUtility.UrlDecode(Request.Params["fileName"]);
if (filePath != null)
{
BigFileDownload(fileName, filePath);
}
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FileName">文件名</param>
/// <param name="FilePath">路径</param>
public void BigFileDownload(string FileName, string FilePath)
{
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
// Length of the file:
int length;
// Total bytes to read:
long dataToRead;
// Identify the file to download including its path.
string filepath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
dataToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(FileName));//System.Text.UTF8Encoding.UTF8.GetBytes(FileName)
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
string message = ex.Message;
this.Page.ClientScript.RegisterStartupScript(GetType(), "Message", "<script>alert('Error : " + message + "');</script>");
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}
}
}
这样当点击文件列表进行下载时:就会将以你传入的文件名作为下载的文件名
<td style="width: 60%; height: 25px">
<a href="javascript:void(0)" onclick="FileDownLoad('../File/aseaglnsdf234dkfauoiomsadfk.doc','新设立外商投资企业区域分布情况报告(国家工商行政管理总局).doc')">新设立外商投资企业区域分布情况报告(国家工商行政管理总局)</a>
- C# javascript文件下载时改变文件名
- C# 文件下载时改变文件名
- C#下载文件时防止文件名出现中文乱码
- 下载 改变 文件名
- 下载文件时,文件名乱码。
- 下载文件时,文件名乱码
- header 改变用户下载时显示的文件名
- c# web 下载文件(解决中文文件名乱码问题)
- 文件下载时 文件名乱码问题
- Django下载文件时,中文文件名问题
- IE文件下载时中文文件名乱码
- 下载文件时,文件名乱码问题
- jsp下载中文文件名文件
- 文件下载中文文件名问题
- 下载文件文件名是加号
- 文件下载中文文件名乱码
- java文件下载文件名乱码
- java文件下载文件名乱码
- 转载:解决采集UTF-8网页空格变成问号乱码
- 应用机器学习的建议(Advice for applying machine learning)
- tableview 按字母排序分区 并加对应副标题
- 禁用页面缓存的几种方法(静态和动态)
- UVA 11538 Chess Queen
- C# 文件下载时改变文件名
- VC6.0 WIN32DLL 如何添加资源
- 数据结构
- IOS应用的产品设计、交互设计理念断想
- ios XIB里的AutoLayout
- android paint 一些效果
- 每秒处理10万订单的支付架构 乐视集团
- 一次XSS攻击过程
- matlab 中 将一组数据归一化到(0,1)之间