C# HttpContext.Current.Response.AddHeader () NOT WORKING

本文关键字:NOT WORKING AddHeader HttpContext Current Response | 更新日期: 2023-09-27 18:03:01

I have a problem with downloading pdf files.

This is my code in c# :

     string file = HttpContext.Current.Server.MapPath("/FOLDER1/FOLDER2/test.pdf");
       HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment; filename='"{0}.pdf'"", "GGG"));
       HttpContext.Current.Response.WriteFile(file, false);
       HttpContext.Current.Response.End();

It seems like HttpContext.Current.Response.AddHeader("Content-Disposition , string.Format("attachment; filename='"{0}.pdf'"", "GGG"));" is not working.

Instead of downloading the file it displays something like this:

x��V}p��۽ۻ�����7�%��.9H�| �����p+w���^B�Z* A��甶Z;���3L�-3-�X�S�t�#S�v:-�~�X�ZK��y�}/��������}��}>~ϳ���$���E���7?���������} �����p4:�z'����9�����b;���X���pP~�+q�둰å| �{p}]"�'ӡ���&� ռ��#�~��H�9�����}��̴���0 +u�mS����� �,��B/.�A��. x���������<�dlls����Elp׷ɷa� ��]���vW_��n}'��ҁY2n�ar���N3VZ��٬t���6���.��Q�u���`s��� �^���-�+�*+ʙ�9�+�$åׯ��{��e�~>�<�|�/

/ArialBold/Encoding/Identity-H/DescendantFonts [43 0 R]/ToUnicode 44 0 R>> endobj 2 0 obj <</Type/Pages/Kids [ 5 0 R ]/Count 1/ProcSet [/PDF

I also tried to downlad an image but the problem remains.

Can someone help me please?

C# HttpContext.Current.Response.AddHeader () NOT WORKING

try something like that:

string file=HttpContext.Current.Server.MapPath("/FOLDER1/FOLDER2/test.pdf");
MemoryStream stream= new MemoryStream();
HttpResponse response = HttpContext.Current.Response;            
response.ClearContent();
response.Clear();
response.ContentType = "application/pdf";
response.AddHeader("Content-Disposition", string.Format("attachment; filename='"{0}.pdf'""));
using (FileStream fs = new FileStream(file, FileMode.Create, System.IO.FileAccess.Write))
 {
     stream.WriteTo(fs);
 }
 response.Flush();
 response.End();