我同时使用tus-node-server和tus-js-client尝试从Web浏览器将文件上传到我的服务器。在较小的文件(10 mb ish)上,它似乎可以正常工作,但是在较大的文件(385 mb ish)上,它似乎由于Access-Control-Allow-Origin
失败而失败。
上传进度将被调用并一直完成,直到100%,然后由于错误而失败。这使我认为该错误与某种类型的验证有关。
在控制台中抛出该错误后,它会重试直到我设置的重试限制。
我在下面发布了错误。有什么原因会发生这种情况?
[Error] Origin https://example.com is not allowed by Access-Control-Allow-Origin.
[Error] XMLHttpRequest cannot load https://upload.example.com//saiudfhia1h due to access control checks.
[Error] Failed to load resource: Origin https://example.com is not allowed by Access-Control-Allow-Origin.
在尝试了所有重试之后,它也因该错误而失败。
tus: failed to upload chunk at offset 0, caused by [object XMLHttpRequestProgressEvent], originated from request (response code: 0, response text: )
前端JS:
var upload = new tus.Upload(file, {
endpoint: "https://upload.example.com/?id=" + res._id,
retryDelays: [0, 1000, 3000, 5000],
metadata: {
filename: res._id,
filetype: file.type
},
onError: function(error) {
console.log("Failed because: " + error)
},
onProgress: function(bytesUploaded, bytesTotal) {
console.log(bytesUploaded, bytesTotal, percentage + "%")
},
onSuccess: function() {
console.log("Download %s from %s", upload.file.name, upload.url)
alert("You have successfully uploaded your file");
}
})
// Start the upload
upload.start()
后端JS:
server.datastore = new tus.FileStore({
directory: '/files',
path: '/',
namingFunction: fileNameFromUrl
});
server.on(EVENTS.EVENT_UPLOAD_COMPLETE, (event) => {
console.log(`Upload complete for file ${event.file.id}`);
let params = {
Bucket: keys.awsBucketName,
Body: fs.createReadStream(path.join("/files", event.file.id)),
Key: `${event.file.id}/rawfile`
};
s3.upload(params, function(err, data) {
console.log(err, data);
fs.unlink(path.join("/files", event.file.id), (err) => {
if (err) throw err;
console.log('successfully deleted file');
});
});
});
const app = express();
const uploadApp = express();
uploadApp.all('*', server.handle.bind(server));
app.use('/', uploadApp);
app.listen(3000);
答案 0 :(得分:0)
原来的问题是服务器位于CloudFlare后面,每个上载请求都有大小限制。设置tus客户端将上载分块为多个请求即可解决此问题。