Following Problem: this Code isn't working in IE9. And in an actual Chrome Browser it is working but the downloaded file is corrupt (the "corrupted file" part also could happen elsewhere ... any suggestion how to do it in IE9 would be nice ).
var file = records[0].data.Files[0].Value.Buffer;
var fileName = records[0].data.Files[0].Value.FileName;
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([file], { type: 'application/octet-stream' }));
a.download = fileName;
document.body.appendChild(a)
a.click();
document.body.removeChild(a)
Related
I'm writing innerHTML of dynamically created SVG-elements to a .txt-file:
function saveCustomSVG(data, filename) {
var blob = new Blob([ data ], { type: "text/plain" });
var url = window.URL.createObjectURL(blob);
var a = document.createElement("a");
a.href = url;
a.download = filename;
a.click();
};
saveCustomSVG(customShapes.innerHTML, "svgCode.txt")
It's working fine, just that all dynamically created stuff gets written in one endless line.
Would it be possible to add a line-break after each closing tag at any stage of the process?
I tried parent.appentChild('br') after each new el, but didn't work.
It currently looks like this:
I have a url which has csv file. i want to download that file by JavaScript. during download i will provide file name and by that name file will be download in client pc. i tried below code which is not working. where i made the mistake. please give me hint.
function downloadFile(fileName, urlData) {
var aLink = document.createElement('a');
var evt = document.createEvent("HTMLEvents");
evt.initEvent("click");
aLink.download = fileName;
aLink.href = urlData;
aLink.dispatchEvent(evt);
}
downloadFile('Test.csv', 'https://testsite.com/targeting/Export/aa.csv');
Use
aLink.click();
instead of the Dispatch-event
I found code which worked. here is the code which i am following now.
var url = 'https://testsite.com/targeting/Export/aa.csv';
var a = document.createElement("a");
a.href = url;
fileName = url.split("/").pop();
alert(fileName);
a.download = fileName;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
a.remove();
I have a problems with Microsoft Edge: when I do the download edge changes the extension of the file that I just have downloaded. With Chrome and Firefox I don't have this problems... I attach the code that I use:
a = document.createElement('a');
a.href = "name.xxx";
a.target = '_blank';
a.download = "name.xxx";
a.type = ".icd";
document.body.appendChild(a);
a.click();
a.remove();
The structure of the file is an XML file, but it has his extension.
Can you help me??
I don't know if I'm late, but this is a solution that works, I had the same error and I was able to solve it.
var blob = new window.Blob([data], { type: 'application/pdf' });
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
var filename = url.substring(url.lastIndexOf('/') + 1);
a.href = url;
a.download = filename + '.pdf' || 'download';
document.body.append(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
I'm using the following code to save file as csv.
$scope.saveCSVFile = function (result)
{
var a = document.createElement('a');
a.href = 'data:application/csv;charset=utf-8,' + encodeURIComponent(result.data);
a.target = '_blank';
a.download = $scope.getFileNameFromHttpResponse(result);
document.body.appendChild(a);
a.click();
$scope.isReportInProgress = false;
};
The file is working on most of the cases but for some reason when the file is larger than 10MB i get "Failed - Network Error".
It happens only on chrome.
I tried to search the web for this issue and couldn't find anything relevant.
Can you think of an idea why does it happens? or maybe use a different save file method that will work on chrome/firefox/IE instead of my function?
I was finally used this one, hope it can help the next one encounter this issue:
var blob = new Blob([result.data], {type: 'text/csv'});
var filename = $scope.getFileNameFromHttpResponse(result);
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);
}
I had a similar problem, but had to serve a zip file, sent from the server in base64 format. What did the trick for me:
function downloadBlob(data, filename) {
const blob = new Blob([new Buffer(data, 'base64')], {type: 'application/octet-stream'});
if (window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveBlob(blob, filename);
} else {
const elem = window.document.createElement('a');
elem.href = window.URL.createObjectURL(blob);
elem.download = filename;
document.body.appendChild(elem);
elem.click();
window.URL.revokeObjectURL(elem.href);
document.body.removeChild(elem);
}
}
return (
{/* ... */}
<a href="#" onClick={() => {downloadBlob(report.zip, `${report.filename}.zip`)}}>
Download <b>{report.filename}.zip</b>
</a>
{/* ... */}
);
I left the function because like the fact that it revokes the URL after serving the file.
I'm new in AngularJS as well as in UI. I'm trying to download XML file. Which is working fine in Google Chrome but in Mozilla Firefox it is not able to download. My JS function is:
response.success(function(data){
var blob = new Blob([data], {type: "application/xml"});
var downloadLink = angular.element('<a></a>');
downloadLink.attr('href',window.URL.createObjectURL(blob));
downloadLink.attr('download', "file.xml");
downloadLink[0].click();
});
I tried with other way and Now It's working in Firefox-
var element = document.createElement('a');
var blob = new Blob([data], {
type: 'text/xml'
});
var url = URL.createObjectURL(blob);
element.href = url;
element.setAttribute('download', 'acl.xml');
document.body.appendChild(element);
element.click();