Dropzone.js 状态为待定且未上传文件
Dropzone.js status is pending and not uploading a file
我正在使用 Multer in back-end to handle file upload and Dropzone.js in front-end. Everything is fine when I use Postman 来测试我的 back-end 代码,但是当我使用 Dropzone 时,状态为待定且文件未上传。然后在等待 4 分钟后我收到超时错误。
我明确表示要使用 POST 而不是 PUT 并立即处理文件上传 queue。
method: "post"
autoProcessQueue: true
我不知道 Dropzone.js 是否有任何我遗漏的选项,或者我的 back-end 代码有问题
这是我处理文件上传的节点代码。
var multer = require('multer');
app.use(multer({ dest: './uploads/'}));
app.post("/attachments/:code", function (req, res, next){
console.log("Posted Files: " + req.files);
console.log("Posted Codes: " + req.params.code);
res.status(200).send("Done");
});
app.get("/attachments/:code", function (req, res, next){
res.status(200);
});
更新:
这是成功上传文件的 Postman 请求的 header:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:8414633
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryuBBzCAdVgFBBCHmB
CSP:active
Host:localhost:3000
Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
这是Dropzone.js的请求的header:
Access-Control-Request-Headers:accept, cache-control, content-type, x-requested-with
Access-Control-Request-Method:POST
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
您可以将 multer 中间件添加到 post 处理程序中:
app.post('/upload',[ multer({
dest: './uploaded/files/',
onError: function (error, next) {
next(error);
}
}
), function (req, res) {
res.status(200).send('success');
}]);
我通过在节点服务器的 Access-Control-Allow-Header
header 中启用 Cache-Control
解决了这个问题。
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Cache-Control, Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'GET,PUT,PATCH,POST,DELETE');
if (req.method === 'OPTIONS') return res.end();
next();
});
我正在使用 Multer in back-end to handle file upload and Dropzone.js in front-end. Everything is fine when I use Postman 来测试我的 back-end 代码,但是当我使用 Dropzone 时,状态为待定且文件未上传。然后在等待 4 分钟后我收到超时错误。
我明确表示要使用 POST 而不是 PUT 并立即处理文件上传 queue。
method: "post"
autoProcessQueue: true
我不知道 Dropzone.js 是否有任何我遗漏的选项,或者我的 back-end 代码有问题
这是我处理文件上传的节点代码。
var multer = require('multer');
app.use(multer({ dest: './uploads/'}));
app.post("/attachments/:code", function (req, res, next){
console.log("Posted Files: " + req.files);
console.log("Posted Codes: " + req.params.code);
res.status(200).send("Done");
});
app.get("/attachments/:code", function (req, res, next){
res.status(200);
});
更新:
这是成功上传文件的 Postman 请求的 header:
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Cache-Control:no-cache
Connection:keep-alive
Content-Length:8414633
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryuBBzCAdVgFBBCHmB
CSP:active
Host:localhost:3000
Origin:chrome-extension://fdmmgilgnpjigdojojpjoooidkmcomcm
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
这是Dropzone.js的请求的header:
Access-Control-Request-Headers:accept, cache-control, content-type, x-requested-with
Access-Control-Request-Method:POST
Origin:http://localhost:9000
Referer:http://localhost:9000/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.124 Safari/537.36
您可以将 multer 中间件添加到 post 处理程序中:
app.post('/upload',[ multer({
dest: './uploaded/files/',
onError: function (error, next) {
next(error);
}
}
), function (req, res) {
res.status(200).send('success');
}]);
我通过在节点服务器的 Access-Control-Allow-Header
header 中启用 Cache-Control
解决了这个问题。
app.use(function (req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Cache-Control, Origin, X-Requested-With, Content-Type, Accept, Authorization');
res.header('Access-Control-Allow-Methods', 'GET,PUT,PATCH,POST,DELETE');
if (req.method === 'OPTIONS') return res.end();
next();
});