在 Alvaro Montoro的帮助下 这是我的 Javascript函数:
function imageloadingdown() {
var url = window.location.href;
var hiddenIFrameId = 'hiddenDownloader';
var iframe = document.getElementById(hiddenIFrameId);
if (iframe === null) {
iframe = document.createElement('iframe');
iframe.id = hiddenIFrameId;
iframe.style.display = 'none';
document.body.appendChild(iframe);
}
iframe.src = url;
}
这是我的 Html代码:
<table>
<tr data-report_id="5">
<td>
<iframe id="hiddenDownloader"/>
<td>
</tr>
</table>
现在我没有服务器端代码: 这是输出:
它将输出附加到当前页面,但我想像谷歌浏览器那样下载它,如下图所示。请帮助我。
答案 0 :(得分:0)
AJAX 不用于下载文件。有多种方法可以实现您的目标:
考虑将这些标题添加到返回文件的页面(来源:https://stackoverflow.com/a/9858790/3695983):
Content-Type:'application/force-download'
Content-Disposition:'attachment; filename=the_filename'
在你的情况下可能是这样的(我没有测试过,你必须这样做):
System.Web.HttpContext.Current.Response.ContentType = "application/force-download;";
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename="+ fileInfo.Name);
您可以在链接上阅读有关这些解决方案的更多信息:
根据上述所有信息,您应该能够解决问题。祝你好运!
答案 1 :(得分:0)
在.aspx
中,您的代码应如下所示:
<asp:Button ID="btnDownload" runat="server" Text="Submit" Style="display: none" OnClick="btnDownload_Click"/>
在.cs
中,您的代码如下:
protected void DownloadFile(string fileName) {
fileName = hdnFileNameDms.Value;
string practiceCode = Profile.PracticeCode;
// string filepath = HttpContext.Current.Server.MapPath(@"~/WEBEHR/DMS/Documents/" + practiceCode + "/" + fileName);
string filepath = hdnFilePath.Value;
FileInfo fileinfo = new FileInfo(filepath);
string webfile = String.Empty;
string[] stringSeparators = new string[] { "WEBEHR", "webehr" };
string[] fileurl = filepath.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
var url = HttpContext.Current.Request.Url.ToString();
string[] weburls = url.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);
if (fileurl.Length > 1 && weburls.Length > 1)
{
webfile = weburls[0] + "webehr" + fileurl[1];
}
if (Request.UserAgent.ToLower().Contains("iphone") || Request.UserAgent.ToLower().Contains("ipad") || Request.UserAgent.ToLower().Contains("mobile"))
{
IpadResponseHelperDMS.Redirect(webfile, "_blank", "menubar=0,width=100,height=100");
return;
}
Response.ContentType = ReturnExtension(fileinfo.Extension.ToLower());
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName.Replace(",", ""));
Response.AddHeader("Content-Length", fileinfo.Length.ToString());
Response.BinaryWrite(File.ReadAllBytes(filepath));
Response.End();
}