[英]ASP.NET download menu from a link
我正在嘗試解決以下問題:
我有一個下載按鈕,當單擊該按鈕時,它會重定向到要下載的文件。 例:
Response.Redirect(“ http://www.example.com/data/file1.zip”);
單擊此按鈕后,會出現一個菜單,您可以按OK進行下載。
但是如果我有以下內容:
Response.Redirect(“ http://www.example.com/data/textfile.txt”);
我得到了一段文字。 我不要這種行為。
當您單擊下載文件時,如何使菜單彈出所有情況。
我嘗試了以下方法:
Response.ContentType = "application/octet-stream";
Response.Redirect("http://www.example.com/data/" + filename);
但是,如果單擊它,我仍然只能在瀏覽器中獲取txt的內容。 有什么方法可以檢查這種mime類型是否真的被應用了嗎?
您需要設置響應類型,
八位字節流應強制用戶打開或保存文件
就像是
public class Download : IHttpHandler {
public void ProcessRequest (HttpContext context) {
string filename = context.Request.QueryString["file"];
string file = context.Server.MapPath(filename);
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename));
try
{
context.Response.TransmitFile(file);
}
catch (Exception ex)
{
SendFailedDownload(filename, context);
}
finally
{
}
}
}
無需執行上述操作,只需使用鏈接標記並將href設置為文件的url。 這將不需要重定向或調整上下文。
<a href="<link to file>" title="">Download me </a>
這將提示您保存或打開...
不適用於txt文件,但適用於其他文件
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.