Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在当前浏览器窗口中下载图像,如Google Chrome_C#_Jquery_Ajax - Fatal编程技术网

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

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) {

在阿尔瓦罗·蒙托罗的帮助下: 这是我的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;
    }
 <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不是用于下载文件的。实现您的目标有多种选择:

  • 弹出一个窗口,其中包含指向地址中文件的链接(或使用window.location.Source:)
  • 有一个隐藏的iframe并将源代码更改为要下载的文件的链接(源代码:)
  • 创建一个表单并发送信息以获取文件(而不是使用AJAX.Source:)
  • 考虑将这些标题添加到返回文件的页面(源:):

    在您的情况下,可能是这样的(我没有测试它,您必须这样做):


    有关这些解决方案的更多信息,请访问以下链接:

    • (我知道这是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并等待有人免费为你工作。