How to provide users a download from my JS app in webview?
Kukulkano opened this issue · comments
What OS are you using (uname -a
, or Windows version)?
I'm on Kubuntu 22.04 LTS (x86_64).
Linux vsdevel 6.2.0-33-generic #33~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 7 10:33:52 UTC 2 x86_64 x86_64 x86_64 GNU/Linux
What programming language are you using (C/C++/Go/Rust)?
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
go version go1.18.1 linux/amd64
What did you expect to see and what you saw instead?
In JS, I use the following code to provide users a decrypted PDF document:
/**
* Offers a user to download the provided byte array
* as file.
*
* @param {string} fileName
* @param {array} byteArray
*/
function offerDownloadByteArray(fileName, byteArray) {
byteArray = new Uint8Array(byteArray);
var blob = new Blob([byteArray], {type: getMimeType(fileName)});
var link = document.createElement('a');
try {
link.href = window.URL.createObjectURL(blob);
link.download = fileName;
link.click();
} catch(e) {
console.error(e);
}
};
In a normal Webbrowser (Firefox, Chromium) it works fine. Sadly, in the webview absolutely nothing happens. It does not even print an error in the console or pops up an error?
Is this the wrong way to provide users with the decrypted file (decryption happens in JS)?
I found other projects solving or having similar issues:
react-native-webview/react-native-webview#3022
https://bugs.chromium.org/p/chromium/issues/detail?id=709986
https://stackoverflow.com/questions/64865972/flutter-webview-blob-pdf-download
Not sure if this is of any help?
As a workaround, I bind a golang function openDownload as JS function:
w.Bind("openDownload", func(filename string, byteArray []byte) {
filename = fileNameWithoutExtTrimSuffix(filename) + "_*" + path.Ext(filename)
file, err := os.CreateTemp("", filename)
if err != nil {
fmt.Println(err)
return
}
if _, err := file.Write(byteArray); err != nil {
fmt.Println(err)
return
}
file.Close()
cmd := exec.Command("xdg-open", file.Name()) // Linux only!
err = cmd.Run()
if err != nil {
fmt.Println(err)
}
})
This can then get called in JS like this:
openDownload("Test.txt", utf8EncodeText.encode("BinaryContentHere));
This works, but I think there should be support for the native blob download methods.
I will try to ask this on Discord. Thanks.