C# javascript文件下载时改变文件名
来源:互联网 发布:duet display windows 编辑:程序博客网 时间:2024/06/04 22:49
在项目中有时会出现下载文件的问题,服务器上保存的文件也许是已GUID命名的文件,但在下载时,也许需要指定一个文件名。我项目中就用到了,到这里做下笔记:
首先在页面上放一个<iframe>标签:
再在js中写如下方法:
注意文件名如果是中文要记得用:js 的encodeURI()方法进行编码传输
首先在页面上放一个<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></td>
效果图如下:
实际上服务器上存的文件是:guid命名的文件
效果图如下:
以上内容摘自http://www.cnblogs.com/joey0210/archive/2011/09/02/2163659.html
在实际运用过程中,可以通过新的aspx页面来达到下载或者生成一个新的文件的目的。
- C# javascript文件下载时改变文件名
- C# 文件下载时改变文件名
- C#下载文件时防止文件名出现中文乱码
- 下载 改变 文件名
- 下载文件时,文件名乱码。
- 下载文件时,文件名乱码
- header 改变用户下载时显示的文件名
- c# web 下载文件(解决中文文件名乱码问题)
- 文件下载时 文件名乱码问题
- Django下载文件时,中文文件名问题
- IE文件下载时中文文件名乱码
- 下载文件时,文件名乱码问题
- jsp下载中文文件名文件
- 文件下载中文文件名问题
- 下载文件文件名是加号
- 文件下载中文文件名乱码
- java文件下载文件名乱码
- java文件下载文件名乱码
- 循环赛日程表的多边形解法
- php验证码类
- flex titleWindow添加最小化和最大化按纽as
- Intellj Idea编译eclipse工程报错:编码 UTF-8 的不可映射字符
- matlab中坐标轴标注和特殊字体
- C# javascript文件下载时改变文件名
- Android NDK 提供的交叉工具链手动编译源码
- hdu 2000
- 1066
- Android屏幕密度(Density)和分辨率的解释
- java媒体框架
- 配置LAMP环境(linux+apache+mysql+php)
- Zend Framework教程-Autoloading介绍
- 窗口操作的一些技巧