所以,我只想创建名称,将文本框中的名称添加到原始文件
的script.js
angular.module('signin').service('fileUpload', ['$http', function ($http)
{
this.uploadFileToUrl = function(file, uploadUrl, name){
var fd = new FormData();
fd.append('file', file);
fd.append('name', name);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
console.log("uploaded");
})
.error(function(){
console.log("fail to upload");
});
}}]);
fileUpload.uploadFileToUrl(file, uploadUrl, $scope.id);
并在app.js
中var storage = multer.diskStorage({
destination: './upload/',
filename: function (req, file, cb) {
cb(null, name + "_"+file.originalname)
}
})
var upload = multer({ storage: storage });
app.post('/move', upload.single('file'), function(req,res,next){
name = req.body.name;
console.log('Uploade Successful ', req.file, req.body);
});
问题是'name'变量的输出在第一次总是取消定义但是大约2秒-1分钟,控制台更新日志并显示multer再次使用'name'变量执行上传文件来自输入的值。因此,它创建了不需要的文件(它保存文件两次,一个是意外文件(原始名称前面有'undefiene'的文件)和我需要的文件),那么如何解决这个问题呢?
答案 0 :(得分:0)
由于我的配置问题,我遇到了同样的问题,请从我的工作代码段再次检查您的配置
//创建存储空间
var location_hotels = multer({ dest: './uploads/images' });
var cpUpload = location_hotels.fields([{ name: 'title', maxCount: 1 },
{name:' text',maxCount:1},{name:' myfile',maxCount:8}]);
router.post('/upload_test', cpUpload, function (req, res, next) {
console.log(req.files); // return if files exist
console.log(req.files); // return if field exist
});
//将数据上传为表单数据,我的角度服务是
angular.module('testServices', [])
.service('MyService', function($http) {
this.uploadFileToUrl = function(file, title, text, uploadUrl){
var payload = new FormData();
payload.append("title", title);
payload.append('text', text);
for(var x = 0; x<file.length; x++) {
payload.append('myfile', file[x]);
}
console.log(payload);
return $http({
url: uploadUrl,
method: 'POST',
data: payload,
//assign content-type as undefined, the browser
//will assign the correct boundary for us
headers: { 'Content-Type': undefined},
//prevents serializing payload. don't do it.
transformRequest: angular.identity
});
}
});
我可以帮助你。
//或者请更改您的代码,然后尝试
router.post('/upload', function(req, res) { // get your post request
profilePictures(req, res, function(err) { // do multer tasks
if (err) {}
}
});
});