nodejs 的多方不解析任何上传的文件
multiparty for nodejs doesn't parse any uploaded file
我正在尝试在后端使用 sails.js 在节点(多方)+ angular (angular-file-upload) 上执行简单的上传文件。前端似乎正确地向后端发送了一个文件,但后端似乎什么也没收到或响应无效请求(不一致)。 form.parse 部分响应为空字段和文件。我几乎被困在这里,谷歌搜索了数小时以找出多方问题,但我找不到任何有用的信息。任何建议将不胜感激
请求header:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:76632
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary7iH8B208bfkavrKt
Cookie:sails.sid=s%3AY9YwxQC90Sq7pc3bp-scW7EH.%2B%2FYHfrTXCUzfPOBpeEt7VFH6Npw3s05Rqyfgu6DHVdg
Host:localhost:1337
Origin:http://localhost:1337
Referer:http://localhost:1337/kirill
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Request Payload
------WebKitFormBoundary7iH8B208bfkavrKt
Content-Disposition: form-data; name="file"; filename="10923270_813578785376550_5210357694589684637_n.jpg"
Content-Type: image/jpeg
------WebKitFormBoundary7iH8B208bfkavrKt--
后端(多方节点):
upload: function(req, res) {
var multiparty = require('multiparty');
var fs = require('fs');
var util = require('util');
var form = new multiparty.Form();
form.parse(req, function(err, fields, file){
if (err) {
res.writeHead(400, {'content-type': 'text/plain'});
res.end("invalid request: " + err.message);
return;
}
console.log(file);
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received fields:\n\n '+util.inspect(fields));
res.write('\n\n');
res.end('received files:\n\n '+util.inspect(file));
});
}
前端(angular 和 angular-file-upload):
$scope.upload = function () {
//console.log($scope.file);
if ($scope.file.data[0]){
console.log("uploading file..");
console.log($scope.file.data[0]);
$upload.upload({
url: 'api/v1.0/upload',
method: 'POST',
file: $scope.file.data[0]
}).progress(function(evt){
var progressPercentage = parseInt(100.0 * evt.loaded/evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.name);
}).success(function (data, status, headers, config){
console.log('file ' + config.file.name + ' uploaded. Response: ' + data.status);
});
}
}
将内容类型设置为多部分:
$upload.upload({
url: 'api/v1.0/upload',
method: 'POST',
data: { 'Content-Type': 'multipart/form-data' },
file: $scope.file.data[0]
}).progress(function(evt){
var progressPercentage = parseInt(100.0 * evt.loaded/evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.name);
}).success(function (data, status, headers, config){
console.log('file ' + config.file.name + ' uploaded. Response: ' + data.status);
});
我更改为在默认情况下使用 sails js 附带的后端使用 skipper。当向后端发送 post 请求时,我需要将 fileFormDataName
指定为 filename
并且我可以使用该名称在后端获取 req.file('filename') 。
我正在尝试在后端使用 sails.js 在节点(多方)+ angular (angular-file-upload) 上执行简单的上传文件。前端似乎正确地向后端发送了一个文件,但后端似乎什么也没收到或响应无效请求(不一致)。 form.parse 部分响应为空字段和文件。我几乎被困在这里,谷歌搜索了数小时以找出多方问题,但我找不到任何有用的信息。任何建议将不胜感激
请求header:
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:76632
Content-Type:multipart/form-data; boundary=----WebKitFormBoundary7iH8B208bfkavrKt
Cookie:sails.sid=s%3AY9YwxQC90Sq7pc3bp-scW7EH.%2B%2FYHfrTXCUzfPOBpeEt7VFH6Npw3s05Rqyfgu6DHVdg
Host:localhost:1337
Origin:http://localhost:1337
Referer:http://localhost:1337/kirill
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36
Request Payload
------WebKitFormBoundary7iH8B208bfkavrKt
Content-Disposition: form-data; name="file"; filename="10923270_813578785376550_5210357694589684637_n.jpg"
Content-Type: image/jpeg
------WebKitFormBoundary7iH8B208bfkavrKt--
后端(多方节点):
upload: function(req, res) {
var multiparty = require('multiparty');
var fs = require('fs');
var util = require('util');
var form = new multiparty.Form();
form.parse(req, function(err, fields, file){
if (err) {
res.writeHead(400, {'content-type': 'text/plain'});
res.end("invalid request: " + err.message);
return;
}
console.log(file);
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received fields:\n\n '+util.inspect(fields));
res.write('\n\n');
res.end('received files:\n\n '+util.inspect(file));
});
}
前端(angular 和 angular-file-upload):
$scope.upload = function () {
//console.log($scope.file);
if ($scope.file.data[0]){
console.log("uploading file..");
console.log($scope.file.data[0]);
$upload.upload({
url: 'api/v1.0/upload',
method: 'POST',
file: $scope.file.data[0]
}).progress(function(evt){
var progressPercentage = parseInt(100.0 * evt.loaded/evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.name);
}).success(function (data, status, headers, config){
console.log('file ' + config.file.name + ' uploaded. Response: ' + data.status);
});
}
}
将内容类型设置为多部分:
$upload.upload({
url: 'api/v1.0/upload',
method: 'POST',
data: { 'Content-Type': 'multipart/form-data' },
file: $scope.file.data[0]
}).progress(function(evt){
var progressPercentage = parseInt(100.0 * evt.loaded/evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.data.name);
}).success(function (data, status, headers, config){
console.log('file ' + config.file.name + ' uploaded. Response: ' + data.status);
});
我更改为在默认情况下使用 sails js 附带的后端使用 skipper。当向后端发送 post 请求时,我需要将 fileFormDataName
指定为 filename
并且我可以使用该名称在后端获取 req.file('filename') 。