Chrome, pdf display, Duplicate headers received from the server

后端 未结 7 960
抹茶落季
抹茶落季 2020-12-14 14:29

I have a section on a website where I display a pdf inside a light box. The recent chrome upgrade has broken this displaying:

Error 349 (net::ERR_RESP

相关标签:
7条回答
  • 2020-12-14 14:52

    I used @roryok's comment, wrapping the filename in quotes:

    Response.AddHeader("content-disposition", "attachment; filename=\"" + FileName + "\"")
    

    @a coder's answer of using single quotes did not work as expected in IE. The file downloaded with the single quotes still in the name.

    0 讨论(0)
  • 2020-12-14 15:07

    The solution above is fine if you don't need to specify the filename, but we wanted to keep the filename default specified for the user.

    Our solution ended up being the filename itself as it contained some commas. I did a replace on the commas with "" and the file now delivers the document as expected in Chrome.

    FileName = FileName.Replace(",", "")
    
    Response.ContentType = "application/pdf"
    Response.AddHeader("content-disposition", "attachment; filename=" & FileName)    
    Response.BinaryWrite(myPDF)
    
    0 讨论(0)
  • 2020-12-14 15:09

    My issue was due to the double quote as shown below:

    var encoding = System.Text.Encoding.UTF8;
    
    *Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**\"{0}\"**", HttpUtility.UrlEncode(file, encoding)));*
    

    Changing the above to this worked!

    *Response.AddHeader("Content-Disposition", string.Format("attachment; filename=**{0}**", HttpUtility.UrlEncode(file, encoding)));*
    
    0 讨论(0)
  • 2020-12-14 15:12

    to fix this for any file type with a custom file name remove the (or similar headers)

    Response.AppendHeader("Content-Disposition", "inline;");
    

    and add

    string fileName = "myfile.xlsx"
    return File(fileStream, System.Web.MimeMapping.GetMimeMapping(Path.GetFileName(filePath)), fileName);
    

    you can also use the filepath instead of a stream in the first parameter

    0 讨论(0)
  • 2020-12-14 15:13

    Had this problem today. Per roryok and others, the solution was to put the filename in quotes.

    Previous, Chrome FAIL:

    header("Content-Disposition: attachment; filename=$file");
    

    Current, Chrome OK:

    header("Content-Disposition: attachment; filename='$file'");
    

    Note the quotes around $file.

    0 讨论(0)
  • 2020-12-14 15:17

    This solution will preserve filename AND open file in browser (.net mvc)

    Response.Headers["Content-Disposition"] = "inline;filename=\"" + theFile.filename + "\"";
    return File(filePath, mimeType);//don't specify filename. It will create second Content-Disposition header
    
    0 讨论(0)
提交回复
热议问题