Skip to content

Commit

Permalink
feat: support datauri download
Browse files Browse the repository at this point in the history
  • Loading branch information
jeasonnow committed Jul 19, 2023
1 parent 79a4860 commit b28509e
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src-tauri/src/inject/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,28 @@ function convertBlobUrlToBinary(blobUrl) {
});
}

function downladFromDataUri(dataURI) {
const byteString = atob(dataURI.split(',')[1]);
// write the bytes of the string to an ArrayBuffer
const bufferArray = new ArrayBuffer(byteString.length);

// create a view into the buffer
const binary = new Uint8Array(bufferArray);

// set the bytes of the buffer to the correct values
for (var i = 0; i < byteString.length; i++) {
binary[i] = byteString.charCodeAt(i);
}

// write the ArrayBuffer to a binary, and you're done
invoke('download_file_by_binary', {
params: {
filename,
binary
},
});
}

function downloadFromBlobUrl(blobUrl, filename) {
convertBlobUrlToBinary(blobUrl).then((binary) => {
invoke('download_file_by_binary', {
Expand All @@ -284,8 +306,10 @@ function detectDownloadByCreateAnchor() {
anchorEle.addEventListener('click', () => {
const url = anchorEle.href;
if (window.blobToUrlCaches.has(url)) {

downloadFromBlobUrl(url, anchorEle.download || getFilenameFromUrl(url));
// case: downoload from dataURL -> convert dataURL ->
} else if (url.startsWith('data:')) {
downladFromDataUri(url);
}
}, true);

Expand Down

0 comments on commit b28509e

Please sign in to comment.