node.js上传文件时如何中断?
How to interrupt while uploading the file in node.js?
我在控制器中有如下代码:
BASE.APP.post('/uploadFile/:request/:file', function(req, res, next) {
var url = req.usersession.webipAddress;
var path = 'uploads/' + req.params.file;
var formData = new BASE.FormData();
formData.append('fileNameUnique', req.params.file);
formData.append('file', BASE.FS.createReadStream(path));
// console.log(formData);
formData.submit(url + '/service/uploadFile/', function(err, response) {
// console.log(response.statusCode);
res.send(response.statusCode);
});
});
如果状态=="cancel"我想中断文件上传,是吗
可能吗?
如果状态 == "cancel" 试试这个:
req.pause()
res.status = 400;
res.end('Upload cancelled');
我不太了解您的代码的工作方式或您的工作流程。这是一个很可能会起作用的通用解决方案。如果您想要更具体的解决方案,请在问题中添加更多代码。
try {
if (status === 'cancel') {
throw new Error("Stopping file upload...");
}
} catch (e) {
res.end("the upload was cancelled because of error: " + e.toString());
}
保存从 formData.submit
返回的值并将其用作调用 request.abort
的句柄。
例如
BASE.APP.post('/uploadFile/:request/:file', function(req, res, next) {
var formData = new BASE.FormData();
// ...
var formSubmitRequest = formData.submit(url + '/service/uploadFile/', function(err, response) {
res.send(response.statusCode);
});
statusChanger.on('status-change', function(status) {
if (status === "cancel" && formSubmitRequest) {
formSubmitRequest.abort();
res.send(524);
}
});
}
来自https://github.com/form-data/form-data:
For more advanced request manipulations submit() method returns http.ClientRequest object
来自https://nodejs.org/api/http.html#http_request_abort:
request.abort()#
Added in: v0.3.8
Marks the request as aborting. Calling this will cause remaining data in the response to be dropped and the socket to be destroyed.
我在控制器中有如下代码:
BASE.APP.post('/uploadFile/:request/:file', function(req, res, next) {
var url = req.usersession.webipAddress;
var path = 'uploads/' + req.params.file;
var formData = new BASE.FormData();
formData.append('fileNameUnique', req.params.file);
formData.append('file', BASE.FS.createReadStream(path));
// console.log(formData);
formData.submit(url + '/service/uploadFile/', function(err, response) {
// console.log(response.statusCode);
res.send(response.statusCode);
});
});
如果状态=="cancel"我想中断文件上传,是吗 可能吗?
如果状态 == "cancel" 试试这个:
req.pause()
res.status = 400;
res.end('Upload cancelled');
我不太了解您的代码的工作方式或您的工作流程。这是一个很可能会起作用的通用解决方案。如果您想要更具体的解决方案,请在问题中添加更多代码。
try {
if (status === 'cancel') {
throw new Error("Stopping file upload...");
}
} catch (e) {
res.end("the upload was cancelled because of error: " + e.toString());
}
保存从 formData.submit
返回的值并将其用作调用 request.abort
的句柄。
例如
BASE.APP.post('/uploadFile/:request/:file', function(req, res, next) {
var formData = new BASE.FormData();
// ...
var formSubmitRequest = formData.submit(url + '/service/uploadFile/', function(err, response) {
res.send(response.statusCode);
});
statusChanger.on('status-change', function(status) {
if (status === "cancel" && formSubmitRequest) {
formSubmitRequest.abort();
res.send(524);
}
});
}
来自https://github.com/form-data/form-data:
For more advanced request manipulations submit() method returns http.ClientRequest object
来自https://nodejs.org/api/http.html#http_request_abort:
request.abort()#
Added in: v0.3.8
Marks the request as aborting. Calling this will cause remaining data in the response to be dropped and the socket to be destroyed.