Fixed saveAs error on Safari. Fixes #252

This commit is contained in:
benweet 2014-01-13 01:38:55 +00:00
parent 27560c73d3
commit 86c58f4cf1
2 changed files with 19 additions and 6 deletions

View File

@ -7,6 +7,7 @@ define([
"settings", "settings",
"eventMgr", "eventMgr",
"fileSystem", "fileSystem",
"core",
"fileMgr", "fileMgr",
"sharing", "sharing",
"classes/Provider", "classes/Provider",
@ -21,7 +22,7 @@ define([
"providers/sshProvider", "providers/sshProvider",
"providers/tumblrProvider", "providers/tumblrProvider",
"providers/wordpressProvider" "providers/wordpressProvider"
], function($, _, constants, utils, storage, settings, eventMgr, fileSystem, fileMgr, sharing, Provider, AsyncTask) { ], function($, _, constants, utils, storage, settings, eventMgr, fileSystem, core, fileMgr, sharing, Provider, AsyncTask) {
var publisher = {}; var publisher = {};
@ -350,7 +351,7 @@ define([
}); });
task.onSuccess(function() { task.onSuccess(function() {
if(pdf !== undefined) { if(pdf !== undefined) {
utils.saveAs(pdf, fileMgr.currentFile.title + ".pdf"); utils.saveAs(pdf, fileMgr.currentFile.title + ".pdf", core.redirectConfirm);
} }
}); });
task.enqueue(); task.enqueue();

View File

@ -216,8 +216,8 @@ define([
}; };
// Export data on disk // Export data on disk
utils.saveAs = function(content, filename) { utils.saveAs = function(content, filename, redirectConfirm) {
if(saveAs !== undefined) { if(saveAs !== undefined && !/constructor/i.test(window.HTMLElement) /* safari does not support saveAs*/) {
if(_.isString(content)) { if(_.isString(content)) {
content = new Blob([ content = new Blob([
content content
@ -228,9 +228,21 @@ define([
saveAs(content, filename); saveAs(content, filename);
} }
else { else {
if(_.isString(content)) {
var uriContent = "data:application/octet-stream;base64," + utils.encodeBase64(content); var uriContent = "data:application/octet-stream;base64," + utils.encodeBase64(content);
window.open(uriContent, 'file'); window.open(uriContent, 'file');
} }
else {
var reader = new FileReader();
reader.onload = function(event) {
redirectConfirm('You are being downloading a PDF file.', function() {
var uriContent = 'data:application/pdf;' + event.target.result.substring(event.target.result.indexOf('base64'));
window.open(uriContent, 'file');
});
};
reader.readAsDataURL(content);//Convert the blob from clipboard to base64
}
}
}; };
// Generates a random string // Generates a random string