gpt4 book ai didi

angularjs - nodejs 的多方不解析任何上传的文件

转载 作者:太空宇宙 更新时间:2023-11-03 23:38:22 26 4
gpt4 key购买 nike

我正在尝试使用 sails.js 在 Node (多方)+ Angular ( Angular 文件上传)上执行简单的上传文件在后端。看起来前端正确地将文件发送到后端,但后端似乎没有收到任何内容或响应了无效的请求(不一致)。 form.parse 部分以空字段和文件响应。我几乎被困在这里,花了几个小时的谷歌搜索来找出多方的问题,但我找不到任何有用的信息。任何建议将不胜感激

请求头:

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--

后端(多方 Node ):

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 ):

$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);
});
}

}

最佳答案

我更改为在后端使用默认使用 sails js 的船长。当向后端发送 post 请求时,我需要将 fileFormDataName 指定为 filename,我可以使用该名称在后端获取 req.file('filename') 。

关于angularjs - nodejs 的多方不解析任何上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28391502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com