客户端异步请求抛出的500友好错误信息,处理起来很困难,需要把后端代码逐个检查下。
若是抛出500错误详细信息,就可以快速把问题处理掉。
如果是响应的是文本,就用 response.text() 方法,JSON 就用 response.json() 方法。
javascript
async function bigFileUpload_click(sef) {
const chunk_size = 1024*1024; // 1MB per chunk
const file = sef.files[0];
const fileSize = file.size;
const totalChunks = Math.ceil(fileSize/chunk_size); // 向上取整
const progressBar = $$('#progressBar');
console.log( file.name );return;
for (let chunkIndex = 0; chunkIndex < totalChunks; chunkIndex++) {
const start = chunkIndex * chunk_size;
const end = Math.min(fileSize, start+chunk_size);
const chunk = file.slice(start, end);
const formData = new FormData();
formData.append('act', 'bigFileUpload_click');
formData.append('file', chunk);
formData.append('fileName', file.name);
formData.append('chunkIndex', chunkIndex);
formData.append('totalChunks', totalChunks);
progressBar.innerHTML = Math.ceil(chunkIndex/fileSize*100);
await fetch(location.pathname,{
method: 'POST',
body: formData
})
.then(response => {
if (response.status !== 200) {
console.log('response:', response, response.text());
}
})
.catch(error => {
console.error('Error:', error)
});
;
}
progressBar.innerHTML = '100%';
}