gpt4 book ai didi

php - Dropzone 和非对象错误

转载 作者:行者123 更新时间:2023-12-01 04:10:04 24 4
gpt4 key购买 nike

我正在尝试在我的网站上实现 dropzonejs 文件上传,但收到此错误

{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function isValid() on a non-object","file":"C:\\Users\\Username\\Desktop\\localhost\\Project\\app\\controllers\\FileController.php","line":147}}

我在 head 标签内有这个。我检查文件是否是图像,然后在将文件发送到 Controller 之前检查尺寸。如果文件不是图像,我会将其直接发送到 Controller 。

<script type="text/javascript">
$(function() {
Dropzone.options.createFile = {
accept: function(file, done) {
// checking if the filetype is an image to check the dimensions
if ((/\.(gif|jpg|jpeg|tiff|png)$/i).test(file.name)) {
var pixels = 0;
var reader = new FileReader();
reader.onload = (function(file) {
var image = new Image();
image.src = file.target.result;
image.onload = function() {
width = this.width;
height = this.height;
if(width < 800 || height < 300) {
done('image is not 800x300 pixels!');
} else {
done();
}
};
});
reader.readAsDataURL(file);
} else {
// if the file is not an image
done();
}
},
url: '/process/create-file',
previewsContainer: ".dropzone-previews",
uploadMultiple: true,
parallelUploads: 100,
maxFiles: 100,
}
});
</script>

这是剥离的 Controller ,我删除了一大块代码以使其更短。因为错误就在顶部。

public function postCreateFile()
{
if(!Input::hasFile('file'))
return Response::json('Where is the file?', 400);

$file = Input::file('file');
if($file->isValid()){
// do stuff
}
}

如果我在 $file->isValid() 行之前添加 return var_dump($file) ,我会得到此响应

array (size=2)  '_token' => string 'BtN7aFkEUQvXlaJDzxx28e4WMI08h5bl3VqrEaHR' (length=40)  'file' =>     array (size=1)      0 =>         object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]          private 'test' => boolean false          private 'originalName' => string '65VWl.jpg' (length=9)          private 'mimeType' => string 'image/jpeg' (length=10)          private 'size' => int 260740          private 'error' => int 0

这是表格。我已将 enctype 和文件设置为 true,但仍然收到错误消息“非对象”,如上所述。

{{ Form::open(array('class' => 'dropzone', 'id' => 'create-file', 'url' => 'process/create-file', 'enctype' => 'multipart/form-data', 'files' => true)) }}
<div class="dropzone-previews" id="giga-drop">
<div class="fallback">
{{ Form::file('file') }}
</div>
</div>
{{ Form::close() }}

知道为什么我会收到此错误吗?

最佳答案

感谢@Maerlyn 指出它是一个数组。我还发现了这个之前搜索时没有注意到的链接Laravel and Dropzonejs, files are not uploaded correctly

我必须将 $file 变量修改为:

$file = Input::file('file'); // didn't work
$file = Input::file('file')[0]; // works

关于php - Dropzone 和非对象错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21659073/

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