Is there any way to implement download attribute in Safari - javascript

I am developing an Calendar App. Back-end will return the file name and ics format string to front-end in JSON. Then I wanna use JavaScript to force download the ics file. This is my code:
function downloadFile(fileName, content) {
var aLink = document.createElement('a');
var blob = new Blob([content]);
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click");
aLink.download = fileName;
aLink.href = URL.createObjectURL(blob);
aLink.dispatchEvent(evt);
}
I pass the fileName and content to this function, and it will generate a link to download this file. This works good in Chrome, but it cannot work in Safari 7. Is it because that safari does not support download attribute? Is there any way to implement this?

Related

To convert blob into .doc, .docx,.xls or .txt to view in browser without download using Javascript

I am able to convert blob into pdf attachment to view in browser without download by following code
var ieEDGE = navigator.userAgent.match(/Edge/g);
var ie = navigator.userAgent.match(/.NET/g); // IE 11+
var oldIE = navigator.userAgent.match(/MSIE/g);
//var bytes = new Uint8Array(response); //use this if data is raw bytes else directly pass resData
var blob = new window.Blob([response], { type: 'application/pdf' });
if (ie || oldIE || ieEDGE) {
window.navigator.msSaveBlob(blob, fileName);
}
else {
var fileURL = URL.createObjectURL(blob);
window.open(fileURL);
}
This is working for pdf in chrome and firefox. But for .doc, .docx,.xlx, it is downloading though i am providing appropriate mime type. ( i.e. for .doc file I am using application/msword and so on).
Note that I am fetching .doc,.docx,.xlx data from secure .net core api. Is there any other way to view word, excel file in browser without download.
This is totally normal behavior, you can't view Word or Excel Documents in your browser by default, that's why the file is just being downloaded. It depends on the browser how files are treated. Old Internet Explorer might just download the PDF too instead of showing it.

base64 string to pdf download

I am facing issue to download pdf in SAPUI5 application. Issue is Getting base64 string from backend system but not able to convert it and display as PDF.
I am able to convert the base64 and download also but only small size.
Not able to download for larger PDF file its downloading but shows download failed.
kindly help me out
var data =" JVBERi0xLjQNJeLjz9MNCjc1MDEgMCBvYmogPDwvTGluZWFyaXplZCAxL0wgOTM2NDM1Mi9PIDc1MDMvRSAxMjE3ODgvTiA1MjIvVCA5MjE0MjgzL0ggWyA2..";
var uri = 'data:application/pdf;base64,' + atob(data);
var link = document.createElement("a");
link.href = uri;
link.style = "visibility:hidden";
link.download = object.FileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
Saving the data as a blob and setting the download link to get the data from the blog may solve your problem for large files. The most effective way in this mechanism is to get the data from your server as binary instead of Base64. It works with base64 too - but it is just a resource over kill in the blob scenario.
var data = Uint8Array.from(atob(base64_string), c => c.charCodeAt(0));
var blob = new Blob([data], {type: "octet/stream"});
var link = document.createElement("a");
link.href = window.URL.createObjectURL(blob);
...
...
As per you current solution, a hyperlink will be created with href contains data:application/pdf;base64,' + base64Data. When the hyperlink is clicked the complete URL will be opened in the browser new tab, which makes the browser to download the PFD file.
If the base64 data is bulk then the browser will take time to download PDF. Sometimes browser will be crashed OR leads to download failed error as it takes too much of time to download.
Alternative Options
Using GET_STEAM method you can download the pdf from the backend only.
Using download plugins like downloadjs, FileSaver.js, StreamSaver.js.
As per you requirement you can get different available plugins for file downloading using client-side JavaScript
Here is a sap blog entry solving your problem.
TLDR:
var base64EncodedPDF = "JVBERi0xLjcNCiW..."; // the encoded string
var decodedPdfContent = atob(base64EncodedPDF);
var byteArray = new Uint8Array(decodedPdfContent.length)
for(var i=0; i<decodedPdfContent.length; i++){
byteArray[i] = decodedPdfContent.charCodeAt(i);
}
var blob = new Blob([byteArray.buffer], { type: 'application/pdf' });
var _pdfurl = URL.createObjectURL(blob);
this._PDFViewer.setSource(_pdfurl);

IOS Safari - How to download or display a pdf

just a simple example, I tried using window.open
var save = 'test'
var blob = new Blob([save], {
type: "application/pdf;charset=utf-8"
});
var fileURL = URL.createObjectURL(blob);
window.open(fileURL);
I also tried using FileSaver.js
var save = 'test'
var blob = new Blob([save], {
type: "application/pdf;charset=utf-8"
});
saveAs(blob, filename);
Assume the blob contains valid pdf content. It seems to work in all other browsers (including OSX safari) by downloading a pdf file.
However in both cases, it seems to open a new tab that looks like this
I want to be able to do something like this where the pdf would open in a new page

How to generate XML file without using ActiveX plugin using javascript

I am facing problem in generating XML file in firefox/chrome using javascript.
I can generate XML file in internet explorer browser , but it uses ActiveX plugin .I want to generate the same on other mentioned browser without using ActiveX.
Anyone can suggest me some good approach ?
Here is an example with blob data. So you can download file generated in browser:
function saveXMLFile(filename, data) {
var blob = new Blob([data], {type: 'text/xml'});
if(window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
}
else{
var elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
document.body.removeChild(elem);
}
}
saveXMLFile('my.xml', '<root><elem>hello</elem></root>');
JSFiddle

Saving CSV file using blob in Safari

I have codes below to generate the download link so that users could download the .csv file on my site.
var link = document.createElement("a");
link.id = "csvDwnLink";
window.URL = window.URL || window.webkitURL;
var csv = "\ufeff" + CSV,
blob = new window.Blob([csv], {type: 'text/csv, charset=UTF-8'}),
csvUrl = window.URL.createObjectURL(blob),
filename = 'export.csv';
$("#csvDwnLink").attr({'download': filename, 'href': csvUrl});
$('#csvDwnLink')[0].click();
document.body.removeChild(link);
I hope the user could click the download link with csvUrl to download the cvs file.
It works on chrome. However, when I click the same link using Safari, it will directly show me the content of the csv file in the tab.
How do I solve this problem so that the safari will show the saving file window which user could select the path where they want to save the file instead of showing the content of the cvs file directly when I click the download link?
Hope someone could me some recommendations or alternative methods.
Thanks in advance!
== Updated ==
Find out solutions here
solution 1,
solution 2
The code will be:
var link = document.createElement("a");
link.id = "csvDwnLink";
document.body.appendChild(link);
window.URL = window.URL || window.webkitURL;
var csv = "\ufeff" + CSV,
csvData = 'data:attachment/csv;charset=utf-8,' + encodeURIComponent(csv),
filename = 'filename.csv';
$("#csvDwnLink").attr({'download': filename, 'href': csvData});
$('#csvDwnLink')[0].click();
document.body.removeChild(link);
Safari will download the file for the user, however, the file name will be unknown, probably it's because Safari don't support 'download' attribute yet as raphael mentioned.
I did a quick research - I looks like Safari does not support what you are trying to achieve.
The reason why your solution works in Chrome (and Firefox) is that they support the download attribute - Safari doesn't yet.
Safari 10.1+ supports "download" attribute. It should work now.
https://github.com/eligrey/FileSaver.js/issues/129#issuecomment-275221240

Categories