一般情况下 txt 文件是向浏览器输出的,应测试人员的要求实现txt文件的下载效果
1、使用Repeater对文件进行列表显示:
其中:
<a href='#'onclick="CreateFrame('<%#Eval("Url")%>')" ><%#Eval("Name") %></a>
2、用户点击时,触发createFrame js脚本 ,代码如下:
function CreateFrame(filepath) {
var FrameName = "uploadFrame_" + Math.floor(Math.random() * 1000);
var oframe = $('<iframe name=' + FrameName + '>');
//修改样式是css,修改属性是attr
oframe.css("display", "none");
oframe.attr("src", "GetFile.aspx?filepath=" + filepath);
//动态创建一个iframe并使src指向下载页面,进行文件的获取
$('body').prepend(oframe);
}
3、GetFile页面
/// <param name="FullName">待下载的全路径</param>
/// <param name="DisPalyName">显示的名称</param>
private void DownLoadFile(string FullName, string DisPalyName)
{
if (File.Exists(Server.MapPath(FullName))){
FileInfo file = new FileInfo(Server.MapPath(FullName));
string saveFileName=file.Name+"."+file.Extension;
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(saveFileName));
Response.TransmitFile(FullName);
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
else{
//文件不存在或已丢失
}
}
ASP.NET中TXT文件的下载实现
2321

被折叠的 条评论
为什么被折叠?



