你可以簡單地使用一個LinkButton回發到服務器並調用以下方法嗎?
internal static void ContentToBrowser(string contentType, string filename, string content)
// Send to browser
var response = HttpContext.Current.Response;
response.Clear();
response.ContentType = contentType;
response.AddHeader("Content-Disposition",
String.Format("attachment; filename=\"{0}\"", filename));
var contentLength = response.ContentEncoding.GetByteCount(content);
response.AddHeader("Content-Length", contentLength.ToString());
response.Write(content);
response.End();
}
這可以讓你所需要的內容直接寫回客戶端,無需擔心存儲在服務器上。例如
protected void LinkButtonClick(object sender, EventArgs e)
{
string filename = @"c:\filepath\blah.xlsx";
string content = File.ReadAllText(filename);
ContentToBrowser("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "downloaded-blah.xlsx", content);
File.Delete(filename);
}
但是,我不得不質疑你之後刪除文件的動機。你怎麼知道它已被客戶端成功下載和存儲?
鏈接是否必須匹配確切格式?這會工作嗎? http://formvalue.co.za/download?file=whatever.xlsx – Nuzzolilo
會怎樣?而不是/做? – Pomster
什麼都不是自己的,這是一個直接的問題... – Nuzzolilo