2016-01-06 145 views
3

在此基礎上:ASP.Net Download file to client browser下載文件到客戶端瀏覽器,在卡住到Response.End()

這也應該工作:(contenu是一個字符串)

 Response.Clear(); 

     Response.ClearHeaders(); 

     Response.ClearContent(); 

     Response.AddHeader("Content-Disposition", "attachment; filename=\"" + txtFileName.Value.ToString() + "\""); 

     Response.AddHeader("Content-Length", contenu.Length.ToString()); 

     Response.ContentType = "text/plain"; 

     Response.Flush(); 

     Response.Write(contenu); 

     Response.End(); 

然而,Response.End()後的瀏覽器似乎反應,因爲我看到它的工作,但然後什麼都沒有發生......

相同的Chrome和資源管理器。

我該怎麼辦?

+0

我懷疑它也可能是一些做的沖洗和寫入的順序;嘗試交換他們的執行ordder – techspider

回答

2

試試這個代碼,它具有稍微不同的參數設置;這是爲我工作

protected void ExportData(string fileName, string fileType, string content) 
    { 
     Response.Clear(); 
     Response.Buffer = true; 
     Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlPathEncode(fileName)); 
     Response.Charset = ""; 
     Response.ContentType = fileType; 
     Response.Output.Write(content); 
     Response.Flush(); 
     Response.End(); 

    } 

這樣調用

ExportData("Report.txt", "text/plain", yourContentString); 
+0

好吧,我只是看到我被回傳愚弄:函數後,意外回發發生,所以標題發送,但因爲頁面重新加載,忽略錯誤,然後忽略,無論如何,你的功能是堅如磐石,所以我會接受你的答案 –

相關問題