ASP.NET adds httphandler to edit the uploaded file name

There is a page in my project DownloadDocument.aspx, and codebhind -DownloadDocument.aspx.cs

In mine DownloadDocument.aspx, I have an anchor that accepts a dynamic link as follows:

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

I want to add httphandler to manage the uploaded file name. How can i do this? Thanks in advance.

+5
source share
3 answers

How to use a common handler (.ashx) for this?

You need to add download information such as file name, contenttyp and the content itself. The pattern should give you a good hat.

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 common handler is called from a URL, for example:

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

, ... HTTPHandler ProcessRequest. . HTTPHandler - web.config.

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

Image, path

:

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

You can try with this code

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

All Articles