2009-04-16 5 views
0

CMS를 만들었으므로 잘되었지만 지금은 모바일 바이너리 (설치 프로그램) 파일을 CMS로 옮기고 싶습니다. 현재 다른 서버에서 스트리밍됩니다.모바일 응용 프로그램 다운로드 사이트

내가 볼 수있는 유일한 해결책은 파일이 어떤 폴더에 있는지 등을 XML 문서로 가지고 파일을 검색하고 모바일 브라우저로 스트리밍하는 데 Linq2Xml을 사용하는 것입니다. 나는 이것을 위해 데이터베이스를 사용하고 싶지 않습니다. 바이트 [], 파일 이름 및 MIME을 지정하여 브라우저에 직접 파일을 스트리밍 할 수있는 기능이 내장되어 있으므로 다운로드 포털을 MVC로 업그레이드하려고합니다.

더 좋은 제안이 있으십니까?

답변

1

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); 
    } 
} 
+1

너무 초라한하지만 사용이, A는 MVC에서 이것에 대한 기능에 FileContentResult라는 내장이 :

DownloadResult는 사용자 정의 ActionResult입니다 새 FileContentResult (바이트를 반환 , "X-EPOC/X-SISX -앱"); – mhenrixon

+0

나는 내가 그것에 충실 할 계획에 대한 불만을 들었으므로 실제로 생산에 들어갔다! 유용한 답변 주셔서 감사합니다 !! – mhenrixon

+0

아, 필자는 FileContentResult가 프레임 워크에 최근에 추가 된 것인지 궁금합니다. 미리보기 릴리스 중 하나를 사용하면서 코드 비트를 얻었어야합니다. 업데이트 된 정보를 보내 주셔서 감사합니다. – Jason

관련 문제