在ASP.NETMVC中返回要查看/下载的文件
SomeRandomFile.bakSomeRandomFile.pdfSomeRandomFile.jpg
FileStreamResult
//Gives me a download prompt.return File(document.Data, document.ContentType, document.Name);
//Opens if it is a known extension type, downloads otherwise (download has bogus name and missing extension)return new FileStreamResult (new MemoryStream(document.Data), document.ContentType);
//Gives me a download prompt (lose the ability to open by default if known type)return new FileStreamResult(new MemoryStream(document.Data),
document.ContentType) {FileDownloadName = document.Name};最新情况:ContentDispositionSystem.Net.Http.Headers.ContentDispositionHeaderValue
using System.Net.Http.Headers;public IActionResult Download(){
Document document = ... //Obtain document from database context
//"attachment" means always prompt the user to download
//"inline" means let the browser try and handle it
var cd = new ContentDispositionHeaderValue("attachment")
{
FileNameStar = document.FileName
};
Response.Headers.Add(HeaderNames.ContentDisposition, cd.ToString());
return File(document.Data, document.ContentType);}// an entity class for the document in my database public class Document{
public string FileName { get; set; }
public string ContentType { get; set; }
public byte[] Data { get; set; }
//Other properties left out for brevity}幕布斯6054654
慕无忌1623718