C# 在当前浏览器窗口中下载图像,如Google Chrome
在阿尔瓦罗·蒙托罗的帮助下: 这是我的Javascript函数:C# 在当前浏览器窗口中下载图像,如Google Chrome,c#,jquery,ajax,C#,Jquery,Ajax,在阿尔瓦罗·蒙托罗的帮助下: 这是我的Javascript函数: function imageloadingdown() { var url = window.location.href; var hiddenIFrameId = 'hiddenDownloader'; var iframe = document.getElementById(hiddenIFrameId); if (iframe === null) {
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;
}
<table>
<tr data-report_id="5">
<td>
<iframe id="hiddenDownloader"/>
<td>
</tr>
</table>
这是我的Html代码:
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;
}
<table>
<tr data-report_id="5">
<td>
<iframe id="hiddenDownloader"/>
<td>
</tr>
</table>
现在我没有服务器端代码:
以下是输出:
它在当前页面中添加了输出,但我想像谷歌chrome一样下载它,如下图所示。请帮助我
AJAX不是用于下载文件的。实现您的目标有多种选择:
有关这些解决方案的更多信息,请访问以下链接:
- (我知道这是PHP,但解决方案适用于任何语言)
有了以上所有信息,您应该能够完全解决问题。祝你发展顺利 在
.aspx
中,您的代码应如下所示:
<asp:Button ID="btnDownload" runat="server" Text="Submit" Style="display: none" OnClick="btnDownload_Click"/>
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();
}
有人请帮帮我…你可以试试这样的东西:。你有没有检查过类似问题的建议解决方案:或@Alvarontoro…谢谢,但它没有下载所附的图像,请帮助我,如何做。如果你不知道答案,那么至少不要否决我的问题1)我没有否决你的问题。2) 我确实有工作要做。3) 我给了你3个与你的问题类似的链接(已解决!)。4) 我不直接知道答案;但如果我在上面工作,我可能会找到它,而且它肯定是我之前发布的链接的混合体。5) 你试过什么?到目前为止,你似乎什么也没做,只是请求StackOverflow并等待有人免费为你工作。