Hello. Has anyone encountered the problem of uploading files larger than 10 MB through fetch [TypeError: Failed to fetch]? The tauri server refused to work with them, and I had to upload them in chunks. Maybe there is some local server setting that increases the limit because if you run the code just in the browser, fetch uploads everything without any extra hassle.
#Fetch 10MB limit problem
33 messages · Page 1 of 1 (latest)
This function does not upload files over 10mb
function fetchImage(url) {
fetch(url).then(response => {
return response.blob();
}).then(blob => {
const blobUrl = URL.createObjectURL(blob);
setImages(blobUrl);
}).catch(error => {
console.error("Ошибка при загрузке изображения:", error);
});
}
If you rewrite like this or fetch with chunks, it already loads.
function fetchImage(url) {
const xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = 'blob';
xhr.onload = function () {
if (xhr.status === 200) {
const blob = xhr.response;
const blobUrl = URL.createObjectURL(blob);
setImages(blobUrl);
} else {
console.error(Ошибка при загрузке изображения: ${xhr.statusText});
}
};
normal fetch in JS
what if you add a console log before the processing of blob ?
Certainly! Here's the translation of your text into English:
I made a test HTML that simply fetches a PNG image larger than 15 MB and creates a blob link, and the code works just in the browser with any files and under tauri with files less than 10 MB, but there's always a problem with files larger than 10 MB.
One moment
i have the same condition
why not giving a try to axios ?
it's just a syntaxic sugar over httpxml req
but with a good dx
I'm using it specifically in the main code, and it's precisely where the problems with large files started, so I'm trying to understand what's wrong.
but the same problem ))
i think you have to add the scope to the tauri config
"allowlist": {
"http": {
"scope": ["https://example.com/*"]
}
}
you can do this https://**/* too. but is not good.
But i'm just guessing...
it would be weird that request under 10MB doesn't have issues with scope
or he could try to use the tauri http client ... not the native fetch() or xhr.
I'm using the tauri client
improt { getClient } from '@tauri-apps/api/http'; // The scope above is for this API
this has also a function Body.
i use this and it works... (using the client to upload files to and other server i build)
If you need some docs here there are: https://tauri.app/v1/api/js/http/
You'll laugh, but if you rewrite the promises with async, the error is gone.
async function fetchImage(url) {
try {
const response = await fetch(url);
const data = await response.arrayBuffer();
const blob = new Blob([data]);
const blobUrl = URL.createObjectURL(blob);
setImages(blobUrl);
} catch (error) {
console.error("Ошибка при загрузке изображения:", error);
}
}
I don't understand anything, but after several tries and restarts, suddenly all the options I tried before started working. I didn't update the packages. I'm baffled...
🤷♂️
The first code works to but there is a async/await inside .then missing ... overall thats basic JS.... <---- MY BAD THIS IS WRONG 😄
But if you need better Performance use this "@tauri-apps/api/http" its also and http client like fetch or so but is rust....