下载图像在当前浏览器窗口像谷歌浏览器
问题描述:
随着阿尔瓦罗·蒙托罗的帮助: 这里是我的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>
现在我没有服务器端代码: 这里是在输出:
它附加在当前页面的输出,但我想下载它像谷歌浏览器确实,如图所示below.Kindly帮助我。
答
在.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();
}
+1
你真棒。 – 2014-10-02 13:11:15
答
AJAX是不下载文件。有实现你想要做什么多个选项:(或使用window.location的来源:https://stackoverflow.com/a/6668806/3695983)
- 流行了在地址链接到文件的窗口。
- 有一个隐藏的iframe并将源代码更改为您要下载的文件的链接(来源:https://stackoverflow.com/a/22455488/3695983)。
- 创建表单并发送信息以获取文件(而不是使用AJAX。来源:https://stackoverflow.com/a/13322848/3695983)。
考虑将这些标头,返回该文件的网页(来源: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);
你可以阅读更多有关这些解决方案上的链接:
- Download file through an ajax call php(我知道这是PHP,但解决方案适用于任何语言)
- how to force download dialog for a text file on the server?
- starting file download with JavaScript
看上面的所有信息应该比能够解决的问题更多。与发展祝你好运!
有人请帮我... – 2014-09-29 15:28:34
你可以尝试这样的事情:http://www.codeproject.com/Articles/55488/File-Download-Using-JavaScript。你有没有检查类似问题的建议解决方案:http://stackoverflow.com/questions/6668776/download-file-through-an-ajax-call-php或http://stackoverflow.com/questions/15092984/how - 从wcf服务流下载文件 – 2014-09-29 15:42:24
@AlvaroMontoro ...谢谢,但它不下载图像像附加的图像,请帮助我,如何做到这一点。 – 2014-09-29 15:50:40