[英]Uploading image file through jQuery with PHP
I have been trying to upload an image to my server from jQuery to PHP, but the uploaded content is always invalid image. 我一直在尝试从jQuery向PHP上传图像到服务器,但是上传的内容始终是无效图像。 I could not identify the error whether I was encoding or decoding it wrong.
无论我编码还是解码错误,我都无法识别该错误。 How can I fix this?
我怎样才能解决这个问题?
$("input[type=file]").change(function(event) {
$.each(event.target.files, function(index, file) {
var reader = new FileReader();
reader.onload = function(event) {
var object = {};
object.filename = document.querySelector('input[type=file]').files[0]['name'];;
object.data = encodeURI(event.target.result);
object.filetype = document.querySelector('input[type=file]').files[0]['type'];
files.push(object);
};
reader.readAsDataURL(file);
});
});
$("form").submit(function(form) {
$.each(files, function(index, file) {
$.ajax({url: "handle.php",
type: 'POST',
data: {fileName: file.filename, data: file.data, fileType: file.filetype},
success: function(data) {
$('#data').html(data);
}
});
});
files = [];
form.preventDefault();
});
if (isset($_POST["data"]) || isset($_POST["fileName"])) {
$data = $_POST["data"];
$fileName = $_POST["fileName"];
$fileType = $_POST["filetype"];
$replace = "data:" . $fileType . ";base64,";
$filedata = str_replace($replace, "", $data); // echo $filedata;
$decodedData = urldecode($filedata); // echo $decodedData;
$fp = fopen('uploads/' . $fileName, "w");
fwrite($fp, $decodedData);
fclose($fp);
}
First make sure you get the Base64 code on the server. 首先,请确保您在服务器上获得了Base64代码。 For example, you can echo, etc.
例如,您可以回显等。
Then assuming your image Base64 code was in $_POST['image']: 然后假设您的图片Base64代码在$ _POST ['image']中:
$data = $_POST['image']
$data = str_replace('data:image/png;base64,', '', $data);
$data = base64_decode($data); // Base64 decoded image data
$source_img = imagecreatefromstring($data);
$imageSave = imagejpeg($source_img, $file, 10);
imagedestroy($source_img);
Using the above lines you can save the image as a file from Base64. 使用上面的行,您可以将图像保存为Base64中的文件。
In jQuery do it this way: 在jQuery中,可以这样操作:
$.each(event.target.files, function(index, file) {
var FR = new FileReader();
FR.onload = function(e) {
object.data = e.target.result; };
FR.readAsDataURL(this.files[0]);
}
FR.readAsDataURL(file);
}
What I see is the you are encoding it wrongly in this line 我看到的是您在这一行中编码错误
object.data = encodeURI(event.target.result);
A few small mistakes, otherwise it's almost working. 一些小错误,否则几乎可以正常工作。
In reader.onload
you are using document.querySelector('input[type=file]').files[0]['name'];
在
reader.onload
您正在使用document.querySelector('input[type=file]').files[0]['name'];
which would select only the first file (see the constant files[0]
). 它将仅选择第一个文件(请参见常量
files[0]
)。 So instead of manually searching you can use file
parameter of each
loop to get filename and type: 因此,您可以使用
each
循环的file
参数来获取文件名并输入以下内容,而不是手动搜索:
var files = [];
$("input[type=file]").change(function(event) {
$.each(event.target.files, function(index, file) {
var reader = new FileReader();
reader.onload = function(event) {
var object = {};
object.filename = file.name;
object.data = encodeURI(event.target.result);
object.filetype = file.type;
files.push(object);
};
reader.readAsDataURL(file);
});
});
In PHP code, a small mistake: 在PHP代码中,有一个小错误:
$fileType = $_POST["filetype"]; // <-- small "t"
urldecode
cannot decode Base64 . urldecode
无法解码Base64 。 For that, use base64_decode
: 为此,请使用
base64_decode
:
$decodedData = base64_decode($filedata); // echo $decodedData;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.