如何下载绑定到gridview的文件
How to download a file bound to an gridview
我错过了一些简单的东西。
我正在生成一些二进制文件,我将它们绑定到GridView。
FileDownloadGrid.DataSource = downloadList;
FileDownloadGrid.DataBind();
网格中有趣的部分是这样编码的:
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="DownloadFile" runat="server" Text="Download" CommandName="DownloadFile1"
CommandArgument='<%#Eval("FullName") +"|" + Eval("Name") %>'>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
我正在尝试使用IFRAME Ajax方法下载文件。
function InitializeRequest(sender, args) {
// Check to be sure this async postback is actually
// requesting the file download.
if (sender._postBackSettings.sourceElement.id == "FileDownloadGrid") {
// Create an IFRAME.
var iframe = document.createElement("iframe");
// Get the desired region from the dropdown.
var fName = $get("CommandArgument").value;
// Point the IFRAME to GenerateFile, with the
// desired region as a querystring argument.
iframe.src = "Default2.aspx?fName=" + fName;
// This makes the IFRAME invisible to the user.
iframe.style.display = "none";
// Add the IFRAME to the page. This will trigger
// a request to GenerateFile now.
document.body.appendChild(iframe);
}
}
从我能在网上找到的,我不能拿起客户端CommandArgument,我似乎不知道如何在脚本中获得'FullName'。
谁能给我指个正确的方向?我为一件应该很简单的事而烦恼。 感谢基因咬牙切齿之后,我决定直接调用JavaScript函数。
javascript:
function DownloadFile(filename) {
// Check to be sure this async postback is actually
// Create an IFRAME.
var iframe = document.createElement("iframe");
// Point the IFRAME to GenerateFile, with the
// desired region as a querystring argument.
iframe.src = "Default2.aspx?fileName=" + filename;
// This makes the IFRAME invisible to the user.
iframe.style.display = "none";
// Add the IFRAME to the page. This will trigger
// a request to GenerateFile now.
document.body.appendChild(iframe);
}
下面是为我工作的代码:
<asp:LinkButton ID="DownloadFile" runat="server" Text="Download"
onClientClick='<%# string.Format("DownloadFile('"{0}'");", Eval("FullName")) %>'></asp:LinkButton>
一个关键位似乎正在将'FullName'路径从'转换为/。
string serverPath = toFileName.Replace("''", "/");
然后在default2.aspx.cs中我这样做:
protected void Page_Load(object sender, EventArgs e)
{
string workPath = Request.QueryString["fileName"];
string fullPath = workPath.Replace('/', '''');
string fileName = Path.GetFileName(fullPath);
string attachmentHeader = String.Format("attachment; filename={0}", fileName);
Response.AppendHeader("content-disposition", attachmentHeader);
Response.ContentType = "application/octet-stream";
Response.WriteFile(fullPath);
Response.End();
}
我相信有更好的方法来完成所有这些,但这是我通过hack想出的,我希望它能帮助到其他人。
相关文章:
- 如何播放部分音频文件
- 从桌面读取python文件时高亮显示代码
- 使用JS如何动态更改显示的html文件中的文本背景颜色
- 如何使用WCF服务和javascript表单post上传.doc文件
- 当包含另一个asp文件时,是否也包含所有引用的样式和脚本页面
- 如何在生成下载文件时显示加载动画
- 有没有任何方法可以将控制器从文件加载到ui路由器$stateProvider中
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 将jsp文件下拉列表中的选定项分配给一个java变量(比如String selection)
- 正在SharePoint 2013母版页中添加JQuery移动文件
- 如何在php文件中获取$.post-ajax传递的值
- 直接下载文件,而不是从window.open(url)
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- ExtJS 5用程序点击actioncolumn gridview
- 使用压缩的JavaScript文件(不是运行时压缩)
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 如何在elfinder插件(一个文件管理器插件)上获得上传前事件
- Javascript运行php文件,然后下载文件
- 如何下载绑定到gridview的文件
- 如何防止用户在gridview中多次点击imagebutton开始下载文件.净Webforms