-
-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
embed_file is limited to 2MB file size in Chrome #23
Comments
Many thanks for the information! It is very helpful. I don't have time at the moment. If anyone wants to help (blob urls seems promising to me), please feel free to send a pull request. |
…hat = 'raw'` and `n = file_size` (could be used in #23)
I did some research on this issue, and there is one challenge that is beyond my capability to solve. I think the implementation would be like below. We generate an <a onclick="function() {var blob = new Blob(new Uint8Array([X]), {type: Y}); this.href = URL.createObjectURL(blob);">
Download
</a> where paste(as.integer(xfun:::read_bin(FILE)), collapse = ',') and The challenge is that the text representation of the
I don't know how to avoid the overhead here. For now, I'll mention the file size limit on the help page. Thanks again! |
I see what you mean. Maybe one option would be to still send the data as This still wouldn't be perfect efficiency-wise, but probably better than sending the text representation of an integer array. (that being said, I'm fine if the limit is 'just' documented) |
I just briefly tested this way of creating the blob url: this.href = URL.createObjectURL(
new Blob(Uint8Array.from(atob(X), c => c.charCodeAt(0)), {type: Y})
); but it didn't seem to work. I'll leave it here and wait for other experts to help. |
I have just implemented into It goes like this: https://github.com/fmmattioni/downloadthis/blob/master/R/utils.R#L44-L67 create_blob <- function(tmp_file, output_file) {
## get type of file
type_file <- mime::guess_type(file = tmp_file)
## read bin
bin_file <- readBin(tmp_file, "raw", 10e6)
bin_file <- jsonlite::toJSON(bin_file, raw = "js")
## produce js function
js_function <- glue::glue(
"
const myBlob = new Blob([{{bin_file}}], { type: '{{type_file}}' });
const downloadURL = window.URL.createObjectURL(myBlob);
const a = document.createElement('a');
document.body.appendChild(a);
a.href = downloadURL;
a.download = '{{output_file}}';
a.click();
window.URL.revokeObjectURL(downloadURL);
document.body.removeChild(a);
",
.open = "{{",
.close = "}}"
)
js_function
} and then the link can be generated like so: htmltools::a(
onclick = create_blob(tmp_file = tmp_file, output_file = output_file)
) |
@fmmattioni Thanks! That is pretty much an implementation of #23 (comment). As I said, the text representation of the file (as a |
@yihui would you recommend getting the blob from the base64 string then? |
the implementation could be like the following: get_data_uri <- function(tmp_file) {
paste0(
"data:",
mime::guess_type(file = tmp_file),
";base64,",
base64enc::base64encode(tmp_file)
)
}
create_blob <- function(tmp_file, output_file) {
base64 <- get_data_uri(tmp_file)
## produce js function
js_function <- glue::glue(
"
fetch('{{base64}}').then(res => res.blob()).then(myBlob => {
const downloadURL = window.URL.createObjectURL(myBlob);
const a = document.createElement('a');
document.body.appendChild(a);
a.href = downloadURL;
a.download = '{{output_file}}';
a.click();
window.URL.revokeObjectURL(downloadURL);
document.body.removeChild(a);
});
",
.open = "{{",
.close = "}}"
)
js_function
} |
That looks better. Thanks for sharing! |
I used
xfun::embed_file
with a several MB large file. While the link works perfectly in Firefox, I get aNetwork error
when trying to download the same file in Chrome.I figured out that there appears to be a 2MB file size limit for
data_url
in Chrome.By filing an issue to this repo, I promise that
xfun::session_info('xfun')
. I have upgraded all my packages to their latest versions (e.g., R, RStudio, and R packages), and also tried the development version:remotes::install_github('yihui/xfun')
.I understand that my issue may be closed if I don't fulfill my promises.
The text was updated successfully, but these errors were encountered: