gpt4 book ai didi

Jquery - CodeIgniter 如何为 blob 提供文件扩展名

转载 作者:行者123 更新时间:2023-12-02 21:07:45 25 4
gpt4 key购买 nike

我遇到过这样的情况,我通过 ajax 将图像 blob 发送到 CI Controller ,CI Controller 将其上传。数据发送完美,但只有当服务器尝试上传文件时才会失败。我注意到它需要带有扩展名的文件类型。由于未提供,因此失败。 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 上传函数返回此错误:

The filetype you are attempting to upload is not allowed.

最佳答案

方法FormData.append有第三个参数,指定用于 blob 的文件名

formData.append('croppedImage', blob, 'featured.jpg');

关于Jquery - CodeIgniter 如何为 blob 提供文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35199449/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com