2014-02-27 63 views
4

下面的代碼工作,讓我下載一個Word文檔,儘快下載文件.....不能在UpdatePanel中

Try 
     Response.BufferOutput = True 
     HttpContext.Current.Response.Clear() 
     HttpContext.Current.Response.Charset = "" 
     HttpContext.Current.Response.ContentType = "application/msword" 
     HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=myfile.doc") 
     HttpContext.Current.Response.Write(s) 
     'HttpContext.Current.Response.End() 
     HttpContext.Current.ApplicationInstance.CompleteRequest() 
     HttpContext.Current.Response.Flush() 
    Catch ex As Exception 
     Response.Write(ex.Message) 
    End Try 

但我添加一個UpdatePanel - 它不下載該文件並沒有錯誤產生的?閱讀後,我添加了一個觸發器的ControlID值設置爲開始創建Word文檔文件的按鈕。我試過幾種代碼組合,但似乎沒有任何工作。有關如何縮小這個範圍的任何幫助?我也調試過,沒有錯誤顯示。我檢查了我的下載文件夾 - 什麼也沒有,嘗試設置沒有緩存(Response.Cache.SetCacheability(HttpCacheability.NoCache)),並沒有奏效。只要我刪除UpdatePanel,那麼所有似乎工作?

<asp:UpdateProgress ID="ProgressUpdate" AssociatedUpdatePanelID="UpdatePanel1" runat="server"> 
     <ProgressTemplate> 
      <img alt="progress" src="../images/loading.gif" /> 
     </ProgressTemplate> 
    </asp:UpdateProgress> 
    <asp:UpdatePanel ID="UpdatePanel1" runat="server"> 
    <Triggers> 
      <asp:PostBackTrigger ControlID="buttonDownloadFile" /> 
    </Triggers> 
     <ContentTemplate> 
     .. 

完全失去了這一個。任何人都可以提出一個解決方法或如何解決這個問題?

+0

這是可以下載使用XML HTTP文件在新的瀏覽器(請參閱[這裏](http://msdn.microsoft.com/en-us/library/ie/hh673569(v = vs.85).aspx)和[here](https://developer.mozilla.org/ en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Handling_binary_data)),但我想象UpdatePanel的代碼(服務器和/或客戶端)根本不支持它。 –

+0

也許不是,但我會想有人可能有這個答案,因爲我的假設是別人會在我做之前嘗試過;-) – Computer

+0

此解決方法是否有幫助? http://encosia.com/ajax-file-downloads-and-iframes/ –

回答

-4

UpdatePanel不支持文件上傳或下載。有很多支持ajax的組件可以做到這一點,Google是你的朋友。

編輯: -

一些例子: -

http://forums.asp.net/t/1076322.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview - 我喜歡這種方法,他注射使用JavaScript指向負責下載文件頁面的iframe中。工作一個UpdatePanel

http://encosia.com/ajax-file-downloads-and-iframes/內 - 類似的方法

+0

謝謝....我不完全確定什麼即時通訊應該搜索?即時通訊使用ASP .Net Ajax控件,但似乎沒有針對什麼即時通訊,除非我錯過了什麼? – Computer

+0

這是不正確的,不應該被接受的答案。 –

6

接受的答案是完全錯誤的。您需要使用scriptmanager註冊控件。礦在主頁面,這裏是我用來註冊任何按鈕的代碼,以便正確發佈郵件。

private void MasterPageRegisterButtonForPostBack(Control bt) 
     { 
      MasterPage masterPage = Master; 
      if (masterPage is MainMaster) 
      { 
       var mainMaster = masterPage as MainMaster; 
       if (bt != null && mainMaster.MasterScriptManager != null) 
       { 
        mainMaster.MasterScriptManager.RegisterPostBackControl(bt); 
       } 
      } 
     } 
+0

我接受的答案是因爲我不知道正確答案是什麼 - 否則我不會問;)。感謝您通過 – Computer

+3

np添加您的答案,我只是不想讓任何人轉向錯誤的方向。 :-) –

+1

我欠你一杯啤酒。 – Joe

0

我得到它的工作方式如下:

我的更新面板裏面我配置了可爲了得到下載工作forece完全回發的控件。

(我使用的也是母版頁,這是相同的解決方案,史蒂夫的,但在ASPX後面對其進行註冊,而不是在代碼中)

<asp:UpdatePanel runat="server" ID="UpdatePanelDownload" UpdateMode="Conditional" ChildrenAsTriggers="True"> 
    <ContentTemplate> 
     <asp:LinkButton ID="LinkButtonDownload" OnClick="Download_Click" runat="Server">Save XML</asp:LinkButton> 
</ContentTemplate> 
    <Triggers> 
    <asp:PostBackTrigger ControlID="LinkButtonDownlod" /> 
    </Triggers> 
</asp:UpdatePanel> 
相關問題