作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我遇到过这样的情况,我通过 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/
我是一名优秀的程序员,十分优秀!