Unable to open download save dialog

后端 未结 2 1903
庸人自扰
庸人自扰 2021-01-22 02:33

Using the below code I am unable to show the open/save as file dialog:

        public void ProcessRequest(HttpContext context)
        {
            string link          


        
相关标签:
2条回答
  • 2021-01-22 03:24

    Finally figured it out. There is actually no problem with the code I posted. As you can see in the Fiddler output, the contents of the text file were successfully written to the response stream and the headers used were also correct. The actual problem comes from how the actual http request was made. I used a

    $.get(urlToGenericHandler);

    request using jQuery. The reason why specifically I am not able to download a file using AJAX or a callback model is beyond the scope of this answer. See supported jQuery datatypes here

    Anyways, I changed the call from using AJAX to using a basic post-back.

    Thanks to all that helped.

    0 讨论(0)
  • 2021-01-22 03:30

    Try changing

    contentType = "application/octet-stream";
    

    to

    contentType = "application/download";
    

    Update: Try swapping the position of the header and content type

    context.Response.AddHeader(
        "Content-Disposition", 
        String.Format("attachment; filename={0}", fileName));
    context.Response.ContentType = contentType;
    context.Response.AddHeader(
        "Content-Length", 
        new FileInfo(fullPath).Length.ToString());
    
    0 讨论(0)
提交回复
热议问题