Search code examples
c#posthttphandler

How can I post parameters from web site to HttpHandler with POST method using C#


I have a HttpHandler to download file. This handler in another server http://localhost:5300 than my website (http://localhost:5400). Now the parameter for download is FileId.

I have on my website a link to specific document for download: http://localhost:5300/App/DownloadFile.axd?FileId=123

I want to add a security token to download. I would like to send this token to handler with POST method not in url like FileId.

How can I create a link or action to download a file with http handler using POST method?

HttpHandler DownloadFile:

public class DownloadFile : IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Clear();
            context.Response.HeaderEncoding = Encoding.UTF8;
            context.Response.ContentEncoding = Encoding.UTF8;
            context.Response.AddHeader("Content-Disposition",
                "attachment; filename=" + HttpUtility.UrlEncode("FileName", Encoding.UTF8)); //return FileName from DB
            context.Response.AddHeader("Content-Length", "Length"); //return Length from DB
            context.Response.Charset = "UTF8";
            context.Response.ContentType = "ContentType"; //return ContentType from DB
            context.Response.BinaryWrite(new byte[]{}); //return binary from DB
            context.Response.Flush();
            context.Response.End();
        }

        #endregion
    }

Solution

  • Client Side

    function CallHandler() {
        $.ajax({
            url: "DownloadFile.ashx",
            contentType: "application/json; charset=utf-8",
            type: 'POST',
            dataType: "json",
            data: JSON.stringify({Field: "123", SecurityToken: "wqe76qw7e6q7we679q6e7qw6e79qwey9"}),
            success: OnComplete,
            error: OnFail
        });
    }
    
    function OnComplete(result) {
        // display something while its downloading -- animation/message
    }
    function OnFail(result) {
        // handle error
    }
    

    Server Side

    public void ProcessRequest(HttpContext context)
    {
        var jsonSerializer = new JavaScriptSerializer();
        var jsonString = String.Empty;
    
        context.Request.InputStream.Position = 0;
        using (var inputStream = new StreamReader(context.Request.InputStream))
        {
            jsonString = inputStream.ReadToEnd();
        }
    
        var workItem = jsonSerializer.Deserialize<List<WorkItem>>(jsonString);
        if(workItem != null && do your check for security token)
        {
                context.Response.ContentType = "application/download";
                context.Response.ContentEncoding = Encoding.UTF8;
                context.Response.AddHeader("Content-Disposition",
                    "attachment; filename=" + HttpUtility.UrlEncode("FileName", Encoding.UTF8)); //return FileName from DB
                context.Response.AddHeader("Content-Length", "Length"); //return Length from DB
                context.Response.Charset = "UTF8";
                context.Response.BinaryWrite(new byte[]{}); //return binary from DB
                context.Response.Write();
                context.Response.Flush();
        }
    }
    
    public class WorkItem
    {
        public string Field { get; set; }
        public string SecurityToken { get; set; }
    }
    

    This should work ;]