Increase Maximum body size for the fetch api in chrome
You should not upload files as strings; that's also valid for the old good XMLHttpRequest
. You would either come to the limits of the server or a browser (that one you are currently facing).
Use multipart upload of a Blob
instead, e. g. like they do here:
const formData = new FormData()formData.append('blob', new Blob(['Hello World!\n']), 'test')fetch('http://localhost:5001/api/v0/add', { method: 'POST', body: formData}).then(r => r.json()).then(data => { console.log(data)})
I found that when posting a large amount of data the use of a Blob
mitigates the out of memory error thrown by firefox and the crashing in chrome.I arrived at Blob
usage after viewing other answers here and here
function performFetch() { const megabytes = document.getElementById( 'megabytes' ).value * 1; const largeString = (new Array(megabytes * 1024 * 1024 )).join("x"); const options = { redirect: 'follow', method: 'POST', body: new Blob( [ largeString ], { type: 'text/plain' } ) }; fetch( 'http://example.com', options ).then( () => { console.log( 'success' ) } ) }