I wanna download some files through js.
The following code works fine when the file has an extension ie. http://example.com/img.jpg, but when it doesn't ie. http://example.com/img it just redirects me to a blank page with the file, as a normal link does.
function downloadURI(uri) {
var link = document.createElement('a');
link.href = uri;
link.click();
}
How do I get over this issue, and make the browser to download?
Simply tell your browser that it's a download:
function downloadURI(uri) {
var link = document.createElement('a');
link.href = uri;
link.download = 'download';
link.click();
}
downloadURI('test')
Related
I have a web based file repository where the files are stored as Base64 in IndexebDB. The page is offline based and used on vessels with poor or no internet connection.
For some reason, the download never ends, causing the file extension to stay as .crdownload and file cannot be opened. This does not happen on my PC with good internet, but I'm not sure why that should matter
var link = document.createElement('a');
var isBlob = input instanceof Blob;
var uri = isBlob ? window.URL.createObjectURL(input) : input;
link.href = uri;
link.download = filename;
document.body.appendChild(link);
link.click();
setTimeout(() => {
window.URL.revokeObjectURL(uri);
document.body.removeChild(a);
}, 100);
I have files in predefine locations on my web application server. It's quite easy to download one with javascript
function downloadURI(uri, name) {
var link = document.createElement("a");
link.download = name;
link.href = uri;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
delete link;
}
But I need to find way to download multiple files as a ZIP file. How to do that?
I want to download pdf file directly without viewing, i have tries following things till now but nothing is helping.
1- window.open("https://s3-link1.pdf", 'Download');
2- <a href="https://s3-link1.pdf" download>Link</a>
Link - https://s3-link1.pdf
Assume my domain is https://www.somedomain.com
I somewhere read we can't download cross-origin files. content-disposition header is required to be passed from backend. I am puzzled here. Another csv file of cross-origin is being downloaded easily.
https://s3-link2.csv
I just need to download pdf file using javascript. Please guide me.
Try with fetch.
fetch("https://s3-link1.pdf", {
method: 'GET'
}).then(resp => resp.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
a.download = "name"; // the filename you want
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
})
Option 1:
with jQuery you can try this:
$.get("https://s3-link1.pdf", function (result)
{
var blob = new Blob([result]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "myFileName.pdf";
link.click();
});
Option 2:
The download attribute works in all modern browsers, including MS Edge, but not Internet Explorer.
In the latest versions of Chrome, you cannot download cross-origin files (they have to be hosted on the same domain).
<a href="https://s3-link1.pdf" download>Download PDF</a>
Find more about it here on this blog: https://actualwizard.com/html-force-download-file
I need to download pdf's from one of our online resources.There is no built in function to batch download.The only way to do it is to navigate to each pdf file, click to open, then click download.
There are several thousand files and this would take a very long time to do.
I got around this in the past using javascript. I gathered all the links to the pdfs, put them in a csv, and had the code loop through each link, download, and move onto the next link.
Unfortunately, I have lost that code and my efforts to recreate it have been unsuccessful.
I have tried everything in this article: How to download PDF automatically using js?
I have tried the code from this article (which I'm pretty sure is what I did before): https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/
This is what I think should work...per the second article I referenced above
function download_file(fileURL, fileName) {
var link = document.createElement('a');
link.href = fileURL;
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
}
var fileURL = "link/to/pdf";
var fileName = "test.pdf";
download(fileURL,fileName);
The code above is just to test download one file from a hardcoded URL. If it worked as intended, when the page is loaded, it should download the pdf from the provided url. Instead, it doesn't do anything on load or refresh.
Any suggestions?
Please check
https://stackoverflow.com/a/18983688/6923146
click me
Another one
https://stackoverflow.com/a/45905238/6923146
function download(url, filename) {
fetch(url).then(function(t) {
return t.blob().then((b)=>{
var a = document.createElement("a");
a.href = URL.createObjectURL(b);
a.setAttribute("download", filename);
a.click();
}
);
});
}
download("https://get.geojs.io/v1/ip/geo.json","geoip.json")
download("data:text/html,Hello Developer!", "HelloDeveloper.txt");
I hope it helpfull
https://www.convertplug.com/plus/docs/download-pdf-file-forcefully-instead-opening-browser-using-js/
You must add link element to DOM
function download_file(fileURL, fileName) {
var link = document.createElement('a');
link.href = fileURL;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
var fileURL = "https://cdn.sstatic.net/clc/img/jobs/bg-remote-header-sm.png";
var fileName = "test.pdf";
download_file(fileURL, fileName); // fix function name
Link must be in same origin
The download attribute on anchor was ignored because its href URL has a different security origin.
Anchor tag not downloading file in IE, instead it gives the option to search for app in app store to open file. For chrome and FF this code is working fine. I don't know this is happening in windows 7 or not as I am using windows 8.1 and windows 7 don't have option for apps.
var a = document.createElement("a");
a.href = filepath;
a.download = filename;
a.click();
Any help will be highly appreciated.
Thanks.
Directly quoting from SOpost
Internet Explorer does not presently support the Download attribute on A tags.
See http://caniuse.com/download and http://status.modern.ie/adownloadattribute; the latter indicates that the feature is "Under consideration" for IE12.
this might help:
var blob = new Blob([response.responseText], { type: headers['content-type'] });
if (navigator.msSaveOrOpenBlob) {
//Launches the associated application for a File or Blob saving for non webkit based browser such as safari or IE
navigator.msSaveOrOpenBlob(blob, "cvSummary.xml");
}
else {
//code for webkit based browser
var link = document.createElement('a');
document.body.appendChild(link);
link.style = "display: none";
var url = window.URL.createObjectURL(blob);
link.href = window.URL.createObjectURL(blob);
link.download = "cvSummary.xml";
link.dataset.downloadurl = ["text/xml", link.download, link.href].join(':');
link.click();
window.URL.revokeObjectURL(url);
}