2010-07-06 5 views

답변

0

EDIT 원본 답장이 제거되었습니다.

존재하는 파일 결과가 콘텐츠 처리를 수정할 수 있는지 확실하지 않지만 "첨부 파일"의 콘텐츠 처리를 강제합니다. 다른 처분을 사용하려면, 나는 사용자 지정 작업 필터를 구현하는 것 : 나는 파일의 실제 바이트 [] 데이터를 전달하기 때문에 내가 이전에 사용했던 하나입니다

/// <summary> 
/// Defines an <see cref="ActionResult" /> that allows the output of an inline file. 
/// </summary> 
public class InlineFileResult : FileContentResult 
{ 
    #region Constructors 
    /// <summary> 
    /// Writes the binary content as an inline file. 
    /// </summary> 
    /// <param name="data">The data to be written to the output stream.</param> 
    /// <param name="contentType">The content type of the data.</param> 
    /// <param name="fileName">The filename of the inline file.</param> 
    public InlineFileResult(byte[] data, string contentType, string fileName) 
     : base(data, contentType) 
    { 
     FileDownloadName = fileName; 
    } 
    #endregion 

    #region Methods 
    /// <summary> 
    /// Executes the result, by writing the contents of the file to the output stream. 
    /// </summary> 
    /// <param name="context">The context of the controller.</param> 
    public override void ExecuteResult(ControllerContext context) 
    { 
     if (context == null) { 
      throw new ArgumentNullException("context"); 
     } 

     HttpResponseBase response = context.HttpContext.Response; 
     response.ContentType = this.ContentType; 
     if (!string.IsNullOrEmpty(this.FileDownloadName)) { 
      ContentDisposition disposition = new ContentDisposition(); 
      disposition.FileName = FileDownloadName; 
      disposition.Inline = true; 
      context.HttpContext.Response.AddHeader("Content-Disposition", disposition.ToString()); 
     } 

     WriteFile(response); 
    } 
    #endregion 
} 

.

+0

죄송합니다. ActionFolter가 아닌 ActionResult (및 ExecuteResult 구현)을 의미했습니다. 수정 된 제목. – DotnetDude

0

그들은 본질적으로 같은 일을합니다. ,

public FileContentResult GetPdf() 
{ 
    return File(/* byte array contents */, "application/pdf"); 
} 

당신이 (당신은 항상 PDF 작업을 수행하려는 경우) 컨텐츠 유형을 지정하는 데에서 자신을 저장하고 싶었을하십시오 FileContentResult 아마 가장 솔직하고 가장 쉬운 시작하는 설정 그 내부에 내용 유형 (application/pdf)을 지정한 ActionResult을 만들어 FileContentResult (PdfContentResult과 같은 것) 대신 반환 할 수 있습니다.

하지만 내가 말했듯이, 그들은 똑같은 일을 할 것이고 성능 차이는 없을 것입니다.

관련 문제