我有这段代码:
public void OpenFile(string fileName)
{
var url = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
using (var fileStream = new FileStream(url, FileMode.Open))
{
byte[] bytes = new byte[fileStream.Length];
int numBytesToRead = (int)fileStream.Length;
int numBytesRead = 0;
fileStream.Read(bytes,numBytesRead, numBytesToRead);
}
}
该代码工作正常,但我想在浏览器中显示该文件,我在文件名中单击执行此方法,参数工作正常,这是我需要的其他代码要在浏览器中显示该文件?大多数文件将是.doc和.pdf。如何在浏览器中显示文档??
答案 0 :(得分:0)
您可以从操作方法返回FileStreamResult
。将方法返回类型从void
更改为ActionResult
public ActionResult OpenFile(string fileName)
{
var pathToTheFile = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
var fileStream = new FileStream(pathToTheFile,
FileMode.Open,
FileAccess.Read
);
return new FileStreamResult(fileStream, MimeMapping.GetMimeMapping(fileName));
}