Jquery - CodeIgniter 如何给 blob 文件扩展名
Jquery - CodeIgniter How to give file extension to a blob
我遇到过这种情况,我通过 ajax 将图像 blob 发送到 CI 控制器,后者会上传它。数据发送完美,但只有当服务器尝试上传文件时,它才会失败。我注意到它需要一个带有扩展名的文件类型。由于未提供,因此失败。 CI 探查器将发布的文件显示为:
Array
(
[name] => blob
[type] => image/png
[tmp_name] => D:xampptmpphpC5EA.tmp
[error] => 0
[size] => 634208
)
虽然它应该是这样的:
Array
(
[name] => featured.jpg
[type] => image/jpeg
[tmp_name] => D:xampptmpphpABDE.tmp
[error] => 0
[size] => 634208
)
我怀疑这是它失败的地方。这是我的上传功能代码:
public function upload__image($path, $image){
$this->output->enable_profiler(TRUE);
$config['upload_path'] = "./assets/images/$path";
$config['allowed_types'] = 'png|jpeg|jpg|gif';
$config['remove_spaces'] = TRUE;
$config['file_name'] = parent::getGUID();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload("$image"))
{
$msg = $this->upload->display_errors('', '');
return false;
}
else
{
$data = $this->upload->data();
return $data['file_name'];
}
}
如有任何帮助,我们将不胜感激。太感谢了。应该在客户端使用 Jquery 还是在服务器端进行修复?也请推荐最佳方法。
更新
$('#saveme').click(function() {
$croppMe.cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
var tid = $('#myid').val();
formData.append('croppedImage', blob);
formData.append('tid', tid);
$.ajax('upload.php', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function (response) {
if(response.Response == 200)
{
console.log("Upload successful"+response.Data);
}
else
{
console.log("some error occured");
}
}
});
});
更新 2
我的php上传功能returns这个错误,我设置的时候显示错误:
The filetype you are attempting to upload is not allowed.
方法 FormData.append
有第三个参数指定用于 blob
的文件名
formData.append('croppedImage', blob, 'featured.jpg');
方法 toBlob 有第二个参数 mimeType。您还应该将文件类型添加为第二个参数。
我遇到过这种情况,我通过 ajax 将图像 blob 发送到 CI 控制器,后者会上传它。数据发送完美,但只有当服务器尝试上传文件时,它才会失败。我注意到它需要一个带有扩展名的文件类型。由于未提供,因此失败。 CI 探查器将发布的文件显示为:
Array
(
[name] => blob
[type] => image/png
[tmp_name] => D:xampptmpphpC5EA.tmp
[error] => 0
[size] => 634208
)
虽然它应该是这样的:
Array
(
[name] => featured.jpg
[type] => image/jpeg
[tmp_name] => D:xampptmpphpABDE.tmp
[error] => 0
[size] => 634208
)
我怀疑这是它失败的地方。这是我的上传功能代码:
public function upload__image($path, $image){
$this->output->enable_profiler(TRUE);
$config['upload_path'] = "./assets/images/$path";
$config['allowed_types'] = 'png|jpeg|jpg|gif';
$config['remove_spaces'] = TRUE;
$config['file_name'] = parent::getGUID();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload("$image"))
{
$msg = $this->upload->display_errors('', '');
return false;
}
else
{
$data = $this->upload->data();
return $data['file_name'];
}
}
如有任何帮助,我们将不胜感激。太感谢了。应该在客户端使用 Jquery 还是在服务器端进行修复?也请推荐最佳方法。
更新
$('#saveme').click(function() {
$croppMe.cropper('getCroppedCanvas').toBlob(function (blob) {
var formData = new FormData();
var tid = $('#myid').val();
formData.append('croppedImage', blob);
formData.append('tid', tid);
$.ajax('upload.php', {
method: "POST",
data: formData,
processData: false,
contentType: false,
success: function (response) {
if(response.Response == 200)
{
console.log("Upload successful"+response.Data);
}
else
{
console.log("some error occured");
}
}
});
});
更新 2 我的php上传功能returns这个错误,我设置的时候显示错误:
The filetype you are attempting to upload is not allowed.
方法 FormData.append
有第三个参数指定用于 blob
formData.append('croppedImage', blob, 'featured.jpg');
方法 toBlob 有第二个参数 mimeType。您还应该将文件类型添加为第二个参数。