我正在尝试使用ng文件上传来上传我的文件但是它们在服务器上上传了一些不同的文件名。我希望它们使用正确扩展名的原始文件名保存(.jpg,.pdf )。 以下是我的代码。 控制器:
$scope.uploadPic = function (file) {
$scope.advert.userDetails={
"name":userDetails.name,
"email":userDetails.email,
"role":userDetails.role
}
file.upload = Upload.upload({
url: '/api/uploaders/uploads',
method: 'POST',
fields: {
details: $scope.advert
},
file: file,
fileFormDataName: 'photo'
});
file.upload.then(function (response) {
console.log("Postcontroller: upload then ");
$timeout(function () {
file.result = response.data;
});
}, function (response) {
if (response.status > 0)
$scope.errorMsg = response.status + ': ' + response.data;
});
file.upload.progress(function (evt) {
// Math.min is to fix IE which reports 200% sometimes
file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
console.log("PostController: upload progress " + file.progress);
});
file.upload.success(function (data, status, headers, config) {
// file is uploaded successfully
console.log('file ' + config.file.name + 'is uploaded successfully. Response: ' + data);
console.log(data);
});
}
API:
var multer = require('multer');
var upload = multer({ dest: 'server/uploads/images'});
答案 0 :(得分:0)
这不是更改文件名的ng-upload,而是更长时间。这是出于安全原因 简而言之,您不希望有人知道他们上传的文件的确切路径 - 如果是恶意的话会怎样?他们可以潜在地执行它。
但是,如果您真的想要,可以在API端执行此操作:
var multer = require('multer');
var storage = multer.diskStorage(
{
destination: 'server/uploads/images',
filename: function (req, file, cb) {
cb(null, file.originalname);
}
}
);
var upload = multer(storage);