How to change browser title while providing file download - javascript

I am trying to set the title of the browser dynamically as the PDF document title. But the title currently gets displayed as the URL of the document.
I'm providing the PDF download as below:
response.setDateHeader("Expires", 0L);
response.setHeader("Content-disposition", "inline;filename=" + title + ".pdf");
response.setContentType("application/pdf");
response.setContentLength(bArray.length);
response.getOutputStream().write(bArray);
How can I set its title in browser?

You can't change the title of the html page which shows your response pdf file. The browser will set the title automatically from the meta data of the pdf which you are returning from your server.
So the simple step to change the title of the pdf preview browser is to set the metadata for your pdf file correctly.
You can find the metadata of your pdf from "File-> properties -> Title"
Browser is showing the title from this information only.

Related

Is it possible with PrintThis .js jquery plugin to send the PDF to storage in a file storage server instead of printing it?

I am trying to use https://jasonday.github.io/printThis/#nada to send a PDF to a folder on my website instead of printing it.
My task is to save form data into a PDF and save the PDF in a folder on my website once the user submits. I like the way PrintThis .js creates the PDF and sends it to print but I need it to send to my folder...maybe through Ajax jquery ?
[enter image description here][1]
details in picture
[1]: https://i.stack.imgur.com/agHee.png

Is it possible to change HTTP-header of a pdf document, you want to embed in a <embed> or <iframe>?

Refering to my other question: Why does pdf document download instead of showing in a embed/iframe?
When i embed a pdf document like the below examples, it downloads the documents instead of showing it embedded in the website.
<embed id="showPdfDocument" src="http://example.com:8080/client/attachment/filename.pdf" type="application/pdf" width="400" height="400">
or
<iframe id="showPdfDocument" src="http://example.com:8080/client/attachment/filename.pdf" style="width:400px;height:400px;"></iframe>
My assumptions:
I guess i have to change the HTTP-Header of the pdf document?
I also guess that the problem is with the field "Content-Disposition: attachment", as shown in the screenshot below
My question:
How can i change the HTTP-Header of the pdf document, using html/javascript only? (Greasemonkey for example)
Screenshot of the HTTP-Header of the pdf document:
("Chrome Dev-Tools(F12)" -> Network Tab, select the pdf document and check the response headers)
The content-disposition header triggers the save dialog. If you want to avoid it without server-side changes, try loading it via XHR and encode it as "data:" link.

Download file from click on image or file link using javascript but it's not work on image

i used this method to download file without show file path in browser tab it's working on docx and pdf file but when i click on image link then it's show the actual path of image which one i don't want so can you help me out this please?
function downloadfile12(file)
{
var baseurl = jQuery("#baseurl").val();
var valFileDownloadPath = baseurl + 'assets/uploads/files/'+file;
window.open(valFileDownloadPath , 'Download');
}
If you want to download an image file in a new tab,you need to add a header Content-Disposition: attachment; filename="filename.jpg" on server side to tell your browser to download the image rather than render it in new tab.
If you just want to download the image file,try download

Display PDF in browser when Content-Disposition is set to attachment

I need to display PDF in browser. The URL of pdf that I'm displaying comes from a server that I do not own. In the response header that service sets 'Content-Disposition' header as 'attachment' and because of that when I open that URL in browsr window it immediately starts downloading instead of displaying.
I want to show this PDF in the browser instead of downloading it but this 'Content-Disposition' header is not letting me do that. Is there any way I can download the contents of PDF and then show it in browser? or use some kind of library to do that?
I tried using pdfobject.com but it is having issues with displaying this URL as well. ( I tried changing the code to display PDF where content-disposition header was not set and that worked)

Prompting user to save file using a 'Save-as' dialog?

I currently have this code:
function download(filename, text) {
var pom = document.createElement('a');
pom.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
pom.setAttribute('download', filename);
pom.click();
}
download('test.html', string);
The string contains a lot of html code that gets written in an .html file.
The above code is working perfectly:
On a button click, the browser (chrome) automatically downloads an html file with the string content written in it.
Now, what I want to do is, instead of chrome downloading the file automatically, it should open a "save-as" dialog box and ask the user the location and name of the file, and then download it to that location.
A quick simple reply would be really appreciated.
My browser was set to automatically download all files in default location which is why not only this file but all other files from my browser were downloaded directly without the save prompt dialogue. Changing the settings in browser to 'always ask the download location' worked.
The only way to do this is to set the header of the file on the server, like so:
<FilesMatch "\.(?i:pdf)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
The download attribute does not allow you to change the filename or filetype any more as it is an obvious security risk.
What you are trying to do it replicate the right-click - save-as dialogue but I'm afraid that is not possible at this time.

Categories