MVC 컨트롤러에서 직접 파일을 제공하는 것은 매우 간단합니다. 여기 말하자면 나는, 이전 준비 하나 :
[RequiresAuthentication]
public ActionResult Download(int clientAreaId, string fileName)
{
CheckRequiredFolderPermissions(clientAreaId);
// Get the folder details for the client area
var db = new DbDataContext();
var clientArea = db.ClientAreas.FirstOrDefault(c => c.ID == clientAreaId);
string decodedFileName = Server.UrlDecode(fileName);
string virtualPath = "~/" + ConfigurationManager.AppSettings["UploadsDirectory"] + "/" + clientArea.Folder + "/" + decodedFileName;
return new DownloadResult { VirtualPath = virtualPath, FileDownloadName = decodedFileName };
}
당신은 (완전히 다른 무언가를, 가능성, 또는) 전달하기 위해 어떤 파일 결정 실제로 조금 더 작업을해야 할 수도 있습니다,하지만 난 그냥 잘라했습니다 재미있는 리턴 비트를 보여주는 예제로 기본에 이릅니다.
public class DownloadResult : ActionResult
{
public DownloadResult()
{
}
public DownloadResult(string virtualPath)
{
VirtualPath = virtualPath;
}
public string VirtualPath { get; set; }
public string FileDownloadName { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (!String.IsNullOrEmpty(FileDownloadName))
{
context.HttpContext.Response.AddHeader("Content-type",
"application/force-download");
context.HttpContext.Response.AddHeader("Content-disposition",
"attachment; filename=\"" + FileDownloadName + "\"");
}
string filePath = context.HttpContext.Server.MapPath(VirtualPath);
context.HttpContext.Response.TransmitFile(filePath);
}
}
너무 초라한하지만 사용이, A는 MVC에서 이것에 대한 기능에 FileContentResult라는 내장이 :
DownloadResult는 사용자 정의 ActionResult입니다 새 FileContentResult (바이트를 반환 , "X-EPOC/X-SISX -앱"); – mhenrixon
나는 내가 그것에 충실 할 계획에 대한 불만을 들었으므로 실제로 생산에 들어갔다! 유용한 답변 주셔서 감사합니다 !! – mhenrixon
아, 필자는 FileContentResult가 프레임 워크에 최근에 추가 된 것인지 궁금합니다. 미리보기 릴리스 중 하나를 사용하면서 코드 비트를 얻었어야합니다. 업데이트 된 정보를 보내 주셔서 감사합니다. – Jason