我使用this code在我的离子应用中上传图片。一切顺利,除了文件名。
文件名变为" .Pic.jpg"
为什么会发生这种情况,我应该怎么做才能解决这个问题? 具体来说,如何使上传图像的文件名与所选图像同名?
答案 0 :(得分:0)
我检查了你被引用的代码。在那里上传的文件,但缺少文件名。为此,您需要提及要上载文件的图像的名称。例如
var options = {
quality: 90,
destinationType: Camera.DestinationType.FILE_URI,
sourceType: Camera.PictureSourceType.PHOTOLIBRARY,
allowEdit: true,
encodingType: Camera.EncodingType.JPEG,
popoverOptions: CameraPopoverOptions,
saveToPhotoAlbum: false
};
$cordovaCamera.getPicture(options).then(function(imageData) {
$scope.profileImageData = imageData;
$scope.profileImageName = Math.floor(Math.random()*16777215).toString(16)
var server = baseUrlImageUpload+'upload.php';
filePath = imageData;
var date = new Date();
var options = {
fileKey: "file",
fileName: $scope.profileImageName,
chunkedMode: false,
mimeType: "image/jpg"
};
$cordovaFileTransfer.upload(server, filePath, options).then(function(result) {
$ionicLoading.hide();
//alert(JSON.stringify(result.response));
}, function(err) {
$ionicLoading.hide();
//alert("Select image again")
var alertPopup = $ionicPopup.alert({
title: 'Sorry!',
template: 'Please select image again.'
});
}, function (progress) {
$ionicLoading.show({
template: 'Updating Image...'
});
});
});
在上面的代码中,我随机传递图像名称 $ scope.profileImageName = Math.floor(Math.random()* 16777215).toString(16)。
你的PHP代码就像这样
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error Code: " . $_FILES["file"]["error"] . "<br />";
} else {
if ($_FILES["file"]["size"] < 1024 * 1024) {
if (file_exists("/files/".$_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. No joke-- this error is almost <i><b>impossible</b></i> to get. Try again, I bet 1 million dollars it won't ever happen again.";
} else {
$number = rand(111111,999999);
move_uploaded_file($_FILES["file"]["tmp_name"], 'images/' .$_FILES["file"]["name"].'.jpg');
echo "Done";
//return $number;
}
} else {
// echo "File Size: " . ($_FILES["file"]["size"] / 1024 * 1024) . " MB<br />";
echo "Please upload below 1MB images";
}
}
?>
我希望这会对你有所帮助: - )