2012-09-07 1 views
5

을 편집 할 HttpHandler를 추가하고이 codebhind 내 DownloadDocument.aspx에서 DownloadDocument.aspx.csASP.NET은 다운로드 한 파일 이름을 내 프로젝트 페이지 <code>DownloadDocument.aspx</code>에있는

이다의이 같은 동적 연결을 앵커가 :

<a id="downloadLink" runat="server" style="margin:5px" 
href="<%# CONTENT_DIRECTORY_ROOT + document.Path %>">Download current file</a> 

다운로드 한 파일 이름을 제어하기 위해 httphandler를 추가하고 싶습니다. 어떻게해야합니까? 미리 감사드립니다.

답변

16

으로 시도 할 수 있습니다 어때요? 이것에 대한 일반 처리기 (.ashx)?

filename, contenttyp 및 콘텐츠 자체와 같은 특정 정보를 추가해야합니다. 샘플은 좋은 시작을 제공해야합니다. 이 같은 일반 핸들러는 URL에서 호출

public class GetDownload : IHttpHandler 
{ 

    public void ProcessRequest(HttpContext context) 
    { 
     if (!string.IsNullOrEmpty(context.Request.QueryString["IDDownload"])) 
     { 
       context.Response.AddHeader("content-disposition", "attachment; filename=mydownload.zip"); 
       context.Response.ContentType = "application/octet-stream"; 
       byte[] rawBytes = // Insert loading file with IDDownload to byte array 
       context.Response.OutputStream.Write(rawBytes, 0, rawBytes.Length); 
     } 
    } 

    public bool IsReusable 
    { 
     get 
     { 
      return false; 
     } 
    } 
} 

:

<a href="/GetDownload.ashx?IDDownload=1337">click here to download</a> 
3

모든 요청이 HTTPHandlerProcessRequest을 거치므로 다운로드하려는 파일의 유형에 따라 다릅니다. 각 요청을 하나씩 확인합니다. HTTPHandler을 프로젝트에 추가해야하며 web.config에 다음과 같이 추가해야합니다.

<httpHandlers> 
    <add path="*.jpg,*.jpeg,*.bmp,*.tif,*.tiff" verb="*" type="NameofYourHandler" /> 
</httpHandlers> 

이것은 모든 Image 유형에 대한 요청을 확인합니다 ..이 path 속성에 언급

편집 :

<add verb="*" path="*DownloadDocument.aspx " type="NameofYourHandler"/> 
+0

내가, 그것은 – Vervatovskis

+0

이 편집 섹션 ...이 다운로드 페이지에 대한 핸들러를 등록합니다 볼 수있는 파일 확장자를 지원하려는 모든 요청에 ​​대해 파일 형식에 관계없이. –

+0

변명 Mayank, 형식에서 나는 처리기 이름 또는 처리기의 전체 경로를 넣습니다. (나는 .NET 환경에서 새로 도입되었습니다) – Vervatovskis

0

당신이 코드

<httpHandlers> 
    <add 
    verb="POST" 
    path="*.jpg,*.jpeg,*.bmp,*.tif,*.tiff" 
    type="YourHandler" /> 
</httpHandlers>