2014-09-29 136 views
-2

隨着阿爾瓦羅·蒙託羅的幫助: 這裏是我的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

它附加在當前頁面的輸出,但我想下載它像谷歌瀏覽器確實,如圖所示below.Kindly幫助我。

enter image description here

+0

有人請幫我... – 2014-09-29 15:28:34

+0

你可以嘗試這樣的事情: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

+0

@AlvaroMontoro ...謝謝,但它不下載圖像像附加的圖像,請幫助我,如何做到這一點。 – 2014-09-29 15:50:40

回答

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(); 
      } 
+1

你真棒。 – 2014-10-02 13:11:15

0

AJAX是下載文件。有實現你想要做什麼多個選項:(或使用window.location的來源:https://stackoverflow.com/a/6668806/3695983

  1. 流行了在地址鏈接到文件的窗口。
  2. 有一個隱藏的iframe並將源代碼更改爲您要下載的文件的鏈接(來源:https://stackoverflow.com/a/22455488/3695983)。
  3. 創建表單併發送信息以獲取文件(而不是使用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); 

你可以閱讀更多有關這些解決方案上的鏈接:

看上面的所有信息應該比能夠解決的問題更多。與發展祝你好運!