簡體   English   中英

如何在ASP.net上顯示另存為對話框

[英]How to show Save As Dialog on ASP.net

我是asp.net的新手,正在開發Online Report Generator,我想讓客戶端選擇他/她想要放置文件的位置,所以這是我的代碼,但似乎沒有用,它返回到我的ajax錯誤語句,我的ConfigurationManager.AppSettings [“ ExtractFolder”]等於“ c:\\ temp \\”:只想問一下代碼出了什么問題?

context.Response.ContentType = "application/vnd.ms-excel";            
context.Response.AddHeader("content-disposition", "attachment;filename="+filename);
context.Response.WriteFile(ConfigurationManager.AppSettings["ExtractFolder"]+filename);
context.Response.Flush();
context.Response.End();

嘗試這個

String FileName = "FileName.txt";
String FilePath = "C:/...."; //Replace this
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "text/plain";
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();

有關更多內容類型,請查看我們的http://en.wikipedia.org/wiki/MIME_type#List_of_common_media_types

首先,檢查要發送給用戶的源文件位於c:\\ temp \\中,並且已將filename設置為要發送的文件的名稱。 接下來,您將要確保您的.net進程具有服務器上c:\\ temp的權限。 可能不是。

另外,請確保您了解response.writefile實際上是從服務器讀取文件並將其寫到瀏覽器中。 您無法指定用戶將文件保存到本地的位置,這是由瀏覽器而非您的代碼處理的。

使用編碼器示例代碼,請確保您更改以下內容(請參閱我的評論)

String FileName = "FileName.xlsx";   //MAKE SURE THIS IS YOUR EXCEL FILENAME ON YOUR SERVER
String FilePath = "C:/...."; //SET THIS TO THE FOLDER THE FILE IS IN (put your file in your root folder of your website for now, you can move it later once you get the download code working)
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //MAKE SURE YOU HAVE CHANGED THIS TOO
response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";");
response.TransmitFile(FilePath);
response.Flush();
response.End();

我還建議先在獨立頁面中嘗試上述操作,然后在知道其工作原理后將其合並到AJAX頁面中。

基本上,您無法按照自己想做的方式去做自己想做的事情。

您擁有的ASP.net代碼取決於完整的http請求/響應周期的一部分。 設置標頭的部分是告訴瀏覽器下載內容的位置。

AJAX請求正在尋找帶有其請求的特定返回值,通常是JavaScript可傳遞的數據。 您沒有返回這個,而是文件的內容。

這是一篇文章 ,對概念和問題進行了一些解釋。 它還為您提供了替代解決方案。 基本上,解決方案圍繞使用iFrame來處理文件下載請求。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM