我正在使用ng-file-upload在网络应用程序中上传图像。
上传过程运行良好,但是当我尝试使用 momentJS 为每个文件指定一个唯一的名称时,它不会自动保留格式并在不带文件扩展名的情况下保存它。
例如,image.jpg 将变为 12-07-2016-23-36-44。
我设法使用以下代码解决了这个问题,但它会将所有图像保存为 JPG,即使它们不是(PNG、GIF...)
$scope.uniquelogo = moment().format('DD-MM-YYYY-HH-mm-ss') + '.jpg';
Upload.upload({
url: 'upload.php',
data: {
file: file,
name: Upload.rename(file, $scope.uniquelogo)
}
如何使用 Upload.rename() 并仍保留原始文件格式?
最佳答案
您可以从原始文件名中检索扩展名并使用它来代替 .jpg
。
function getFileExtension(filename) {
return filename
.split('.') // Split the string on every period
.slice(-1)[0]; // Get the last item from the split
}
$scope.uniquelogo = moment().format('DD-MM-YYYY-HH-mm-ss');
Upload.upload({
url: 'upload.php',
data: {
file: file,
name: Upload.rename(file, $scope.uniquelogo + getFileExtension(file.name))
}
...
关于javascript - 使用 AngularJS 和 ng 文件上传保持文件重命名的原始格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38339514/