Need to download a file from webpage to local drive - javascript

I has a requirement like when user clicks on a download button instead of showing the content in browser, i want to save it to the localdisk(perticular location) of the user desktop. Is it possible to do??
If yes,Please help me with possibilities..
Thanks in advance

No a website can't decide where it can save something. Everything goes to download folder by default. You have to be using some sort of plugin with permissions or make like browser addon/extension.
If you want to prompt download then you could set send headers in php:
Content-Disposition: attachment; filename="fname.ext"
and
Content-Type: application/force-download
Or you could set attribute download to link in html
<a href="file.abc" download>Click Me</a>
Utility of HTTP header "Content-Type: application/force-download" for mobile?
http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
http://www.php.net/manual/en/function.header.php
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Attributes

You shall just point the file name like this to download.
But you cannot decide the path by your code.
Download the File
Note : Do not try this in jsfiddle or in codepen because they will rename the link with their custom so that the file will be displayed within their output. So, try it in your web server or in your localhost.

Related

Javascript Check whatever link is download link or webpage link

It there anyway to check link whatever is a download link like http://ipv4.download.thinkbroadband.com/5MB.zip
or normal web like open https://www.google.com
Currently I am using Electron and want to check if link is download link it will download and not open new browser but if not it will open in browser window
Make an HTTP request and check the Content-Type and (if it is there) Content-Disposition headers.
If Content-Disposition says it is an attachment, then it is a download. If it says it is inline, then it is intended to be displayed in the browser window.
If Content-Disposition isn't specified, then you'll need to make the decision based on the MIME type. e.g. text/html should probably be shown in the browser window.
May be check the end of the link... if it ends with .zip .jpg .pdf .... it is a file.
Or .html ... it is a page.

Open default mail box with attachment using jsp

I am trying to open the default mail box with the pdf attachment, using my jsp button with the following script in jsp
<span>Email </span>
But it is able to open the default mail box with blank email, but not able to get the attachemnt from the url: ${pageContext.request.contextPath}/report_generate_pdf_ajax_call
I took the help of following link Link1 and Link2Link3 but I haven't got success with my result.
Any help regarding in this context will be appriciated.
No, you can not add an attachment to a message with the mailto.
mailto: only supports header values or text/plain content.
There is no provision for it in the mailto: protocol, and it would be
a gaping security hole if it were possible. The best idea to send a
file is:
Have the user to choose a file
Upload the file to a server and the server return a random file name after upload
Build a mailto: link that contains the URL to the uploaded file to the server in the message body
So probably you can use apache commons for this but there will be a place on the server where all the user files will be located that will require cleaning from time to time somehow.
For more info kindly see here

Change file name when using window.location to download

I'm using window.location to download my image. It isn't in HTML because I generate the image on the server and then send it back down so it looks like :
window.location = data.url;
I've seen a few other questions but they suggest the download attr which I don't have because there's no HTML.
Is there a way I can change the file name?
Front-end solution
The only thing you can do on the front-end side is to change your code to HTML <a> element with download attribute:
Download
When user clicks this link, the browser forces download and saves the file with given filename. You can read more about it in this post. It's quite a new feature so check the browser support.
Back-end solution
If you can modify the server-side code then you should use content-disposition header as defined in RFC 2183.
content-disposition: attachment; filename=very_important_report.pdf
I've been wondering about it as well and saw this post but I was also using vuejs for the project and want the export to be continues even when switching from one page to another so I tried something and it did work here is another solution:
var link = document.createElement('a');
link.setAttribute('href', '<yourlink_or_data>');
link.setAttribute('download', 'filename.ext');
link.click();
You can't change the filename on the client side. You would have to do that on the server.
You could set the content-disposition header (on the server side) like this:
Content-Disposition: attachment; filename="yourname.gif"

detecting Download file dialog?

is there any way i could detect opening of download file dialog box on web pages like hyperlink click event occurs and download file dialog box appears.... ??
and can i edit the filename in it ...... like attaching some website name along with filename .... so when the user download any file it automatically rename in to website-filename.pdf etc pro-grammatically
can we use input tag for it ?? or have to make customcontrols for it ??
thanks if any help provided
take care.
regards,
newbiefreak
You can just make a hyperlink with its href to a regular file, your browser will prompt to download it.
As for renaming the file, all you could do is create a special page which sends the file contents and correct headers, and specifying another name. You'll have to send the content-disposition header, as such:
Content-disposition: attachment; filename=yourfilename.extension
You can send a Content-disposition header to force a file downlaod box and specify a default filename.
http://support.microsoft.com/kb/260519
In regards to editing the filename:
HTML5 introduces a new attribute for a tags: download.
Using it forces browsers that support the attribute to prompt for a file download, rather than navigating to or attempting to open the linked file.
Also, whatever you value you assign to download will replace the file's actual name.
Source and demo: http://updates.html5rocks.com/2011/08/Downloading-resources-in-HTML5-a-download

Prompting for download

Whats the best method to prompt a user to download something? In the past I've used window.open('file.pdf'); but I can see popup blockers having a problem with this. Of course I'll include a manual link aswel.
I basically want something like the Microsoft Download page. So whats the script that prompts this?
Redirect using javascript.
function redirect() {
window.location = 'http://www.url.to/your.file';
}
Content-Disposition: attachment; filename="file.pdf"
To do this you may want to pass your file.pdf through a server script that forces that header on it.
What you see on that download page is just a location change. And if that page returns the download header the browser won't change page.
Set a content disposition header with an attachment value

Categories