在当前浏览器窗口中下载图像,如Google Chrome

时间:2014-09-29 14:49:25

标签: c# jquery ajax

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>

现在我没有服务器端代码: 这是输出: enter image description here

它将输出附加到当前页面,但我想像谷歌浏览器那样下载它,如下图所示。请帮助我。

enter image description here

2 个答案:

答案 0 :(得分:0)

AJAX 用于下载文件。有多种方法可以实现您的目标:

  1. 弹出一个窗口,其中包含指向该地址文件的链接(或使用window.location。来源:https://stackoverflow.com/a/6668806/3695983)。
  2. 拥有隐藏的iframe并将源更改为您要下载的文件的链接(来源:https://stackoverflow.com/a/22455488/3695983)。
  3. 创建表单并发送信息以获取文件(而不是使用AJAX。来源:https://stackoverflow.com/a/13322848/3695983)。
  4. 考虑将这些标题添加到返回文件的页面(来源: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();
            }