“name” web pdf for better default save filename in Acrobat?

后端 未结 16 1763
难免孤独
难免孤独 2020-12-14 00:09

My app generates PDFs for user consumption. The \"Content-Disposition\" http header is set as mentioned here. This is set to \"inline; filename=foo.pdf\", which should be

16条回答
  •  余生分开走
    2020-12-14 01:03

    File download dialog (PDF) with save and open option

    Points To Remember:

    1. Return Stream with correct array size from service
    2. Read the byte arrary from stream with correct byte length on the basis of stream length.
    3. set correct contenttype

    Here is the code for read stream and open the File download dialog for PDF file

    private void DownloadSharePointDocument()
    {
        Uri uriAddress = new Uri("http://hyddlf5187:900/SharePointDownloadService/FulfillmentDownload.svc/GetDocumentByID/1/drmfree/");
        HttpWebRequest req = WebRequest.Create(uriAddress) as HttpWebRequest;
        // Get response   
        using (HttpWebResponse httpWebResponse = req.GetResponse() as HttpWebResponse)
        {
            Stream stream = httpWebResponse.GetResponseStream();
            int byteCount = Convert.ToInt32(httpWebResponse.ContentLength);
            byte[] Buffer1 = new byte[byteCount];
            using (BinaryReader reader = new BinaryReader(stream))
            {
                Buffer1 = reader.ReadBytes(byteCount);
            }
            Response.Clear();
            Response.ClearHeaders();
            // set the content type to PDF 
            Response.ContentType = "application/pdf";
            Response.AddHeader("Content-Disposition", "attachment;filename=Filename.pdf");
            Response.Buffer = true;
            Response.BinaryWrite(Buffer1);
            Response.Flush();
           // Response.End();
        }
    }
    

提交回复
热议问题