PDFViewer - How to set the filename when downloading from a stream?

0
0

I am loading a PDF into the PDFViewer from a stream but would like to set the filename of the file when downloaded by the user from the browser. By default it seems to be “document.pdf”. I’ve tried a couple of things unsuccessfully:

  1. Looking at the sample of code from Luca in this question, https://wisej.com/support/question/pdf-viewer-question, it seems I should just be able to set PDFSource to the filename and have the control still load from the stream but this does not work, I get an error displayed in the PDF viewer:

PDF.js v2.0.493 (build: c5c06bf5)
Message: PDFDocument: stream must have data

2. So I decided to change to using a Postback url. I removed the PDF stream and set the PDFSource = this.GetCallbackURL(); This also causes the same error as above in the PDFViewer control and never calls my ProcessRequest method. Is this because the control is calling its original handler rather than mine?

Any suggestion as to how I can set the filename?

TIA

Nic

 

 

  • Luca (ITG)
    PdfSource is already rendered to the client as GetPostbackURL().
  • Nic Adams
    I already saw that in https://wisej.com/support/question/pdf-viewer-question – from the sample of code you gave it looked to me as if I could set the PDFSource and WiseJ would use the filename when building the stream: response.AppendHeader(“Content-Disposition”, new ContentDisposition() { DispositionType = “inline”, FileName = Path.GetFileName(this.PdfSource) }.ToString());
  • You must to post comments
0
0

So setting filename in the handler didn’t work, but I’ve since discovered that there is a method on pdf.js that can set the filename:

PDFViewerApplication.setTitleUsingUrl() which takes a URL and then figures out the filename from it.

My problem now is that I cannot figure out how to access the PDFViewerApplication object. I’ve tried to do this from OnWebRender and from other event using Eval(string.Format(“PDFViewerApplication.setTitleUsingUrl(‘{0}’);”, Application.Url + fileName)); but I always get “PDFViewerApplication is undefined”

Unfortunately I’m not a js programmer so my understanding is very limited. Can anyone point me in the right direction?

TIA

Nic

  • You must to post comments
0
0

Thanks both,

But still slightly confused. To use Application.Download I need to get the PDFViewer to call my handler. I’ve set:

pdfViewer1.PdfSource = this.GetPostBackURL();

but when the viewer is rendered it displays the same error as in my original post:

PDF.js v2.0.493 (build: c5c06bf5)
Message: PDFDocument: stream must have data

and my ProcessRequest(HttpContext context) handler is never called.

I’ve built and attached a simple example. You will see that the NotImplimentedException in the handler is never reached.

  • Luca (ITG)
    That’s because PdfSource is either URL or a file and the PdfViewer control already used GetPostbackURL(). When you set it to postback.wx/…. it doesn’t recognize it to be a URL and it tried to load a nonexistent file. If you want to use your handle you can try this PdfSource = Application.Url + this.GetPostbackURL(). You can also simply override the IWisejHandler interface in a subclass of PdfViewer and don’t change PdfSource.
  • Nic Adams
    Thanks Luca, to get my “override” to work I had to subclass – just adding Application.Url wasn’t reliable
  • You must to post comments
0
0

Hi Nic,

the Mozilla PDF viewer and probably most other PDF viewser don´t seem to care about the attachment name header.
They use the PDF title in the PDF when it´s set. I´m afraid we can´t change that.

Best regards
Frank

  • You must to post comments
0
0

Hi Nic,

 

To set the file name use

Snippet

Application.Download("Wisej-Datasheet-V2.2.pdf", "MyFileName.PDF");

Using a Stream, use
Snippet
Application.Download(stream, "MyFileName.PDF");
  • You must to post comments
Showing 4 results
Your Answer

Please first to submit.