我正在開發一個項目,該項目需要能夠讓用戶從服務器上的靜態位置下載pdf。我正在閱讀來自this網站的說明,這是一箇舊帖子,我注意到他們在更新中指定了微軟的MVC框架早已包含在內,並且Action Result允許他們討論的相同功能因此使其過時,我看過有點在線,但一直沒能找到任何討論這種內置功能的資源。如果任何人有任何討論這個問題的鏈接或其他信息,這將是非常有幫助的。謝謝。在ASP.Net中下載文件MVC網絡應用程序
0
A
回答
1
您可以使用FileResult
而不是ActionResult
在響應中返回文件流。舉一個例子,看看SO上的Can an ASP.Net MVC controller return an Image?問題。
0
返回FileResult
。
1
public ActionResult Show(int id) {
Attachment attachment = attachmentRepository.Get(id);
return new DocumentResult { BinaryData = attachment.BinaryData,
FileName = attachment.FileName };
}
使用這個自定義類,大概類似於FileResult:
public class DocumentResult : ActionResult {
public DocumentResult() { }
public byte[] BinaryData { get; set; }
public string FileName { get; set; }
public string FileContentType { get; set; }
public override void ExecuteResult(ControllerContext context) {
WriteFile(BinaryData, FileName, FileContentType);
}
/// <summary>
/// Setting the content type is necessary even if it is NULL. Otherwise, the browser treats the file
/// as an HTML document.
/// </summary>
/// <param name="content"></param>
/// <param name="filename"></param>
/// <param name="fileContentType"></param>
private static void WriteFile(byte[] content, string filename, string fileContentType) {
HttpContext context = HttpContext.Current;
context.Response.Clear();
context.Response.Cache.SetCacheability(HttpCacheability.Public);
context.Response.ContentType = fileContentType;
context.Response.AddHeader("content-disposition", "attachment; filename=\"" + filename + "\"");
context.Response.OutputStream.Write(content, 0, content.Length);
context.Response.End();
}
}
相關問題
- 1. 使用struts2在網絡應用程序中上傳文件並下載
- 2. R網絡抓取,從網絡應用程序下載數據
- 3. 在asp.net下載文件mvc
- 4. 在ASP.NET 3.5網站子文件夾下部署ASP.NET MVC 3應用程序
- 5. ASP.NET程序文件下載
- 6. 將文件內容加載到網絡應用程序中
- 7. 如何在WinForm應用程序中從網上下載文件
- 8. 網絡應用程序文件上傳
- 9. asp.net mvc下載文件
- 10. MVC應用程序中的文件下載
- 11. 集成星號到我的asp.net網絡mvc應用程序
- 12. Asp.net多語言網絡應用程序
- 13. 如何在ASP.NET MVC Web應用程序中傳遞上下文
- 14. 如何將文件從網上下載到ASP.NET Web應用程序中?
- 15. 示例Grails網絡應用程序:上傳文件,處理它,下載
- 16. 如何在ASP.Net Form網站中部署ASP.Net MVC應用程序
- 17. 從網頁在WP7應用程序下載文件
- 18. 在asp.net MVC應用程序
- 19. 蟒蛇程序的網絡界面:上傳和下載文件
- 20. 從網絡下載文本文件
- 21. 使用AngularJS和$ http.post下載在MVC應用程序中的文件
- 22. Python網絡應用程序,可以下載本身
- 23. ClickOnce應用程序文件下載組
- 24. 從rails應用程序下載文件
- 25. 從silverlight應用程序下載文件
- 26. 從應用程序下載「mobileconfig」文件
- 27. 網絡掛接在Asp.net MVC
- 28. ASP.Net - 無法從Web應用程序訪問下載文件夾
- 29. 無法從asp.net中的應用程序文件夾下載文件
- 30. Asp.net Intranet應用程序在本地網絡中無IP
感謝所有爲你的建議,我會努力實現基於所提供的信息的演示。 – kingrichard2005 2010-05-06 18:12:13