我的问题是我想将dropzone与cakephp 2.5集成;但提交表格后 $ this-> request-> data ['pic'] 返回空数组。但是当我添加图像时,在放置区域;它显示了成功的形象。第二;文件未移至目标路径“ APP.'webroot'.DS.'uploadedpics'.DS ”
请检查下面的代码。
我在默认布局中包含了dropzone css和js文件
echo $this->Html->css('dropzone/basic');
echo $this->Html->css('dropzone/dropzone');
echo $this->Html->script('js/dropzone/dropzone');
echo $this->Html->script('js/dropzone/dropzone.min');
echo $this->Html->script('js/dropzone/dropzone-amd-module');
echo $this->Html->script('js/dropzone/dropzone-amd-module.min');
我的HTML页面有form和dropzone js: -
<form method="post" action="/domain/controller/addpics" class="dropzone" id="mydropzone" enctype='multipart/form-data'>
<div id="dropzonePreview"></div> <!--// this will the dropzone drag and drop section .notice we have given it an id dropzonePreview .-->
<input type="button" id="sbmtbtn" value="submit"/>
</form>
<script>
Dropzone.options.mydropzone = {
// url does not has to be written if we have wrote action in the form tag but i have mentioned here just for convenience sake
url: '/domain/controller/addpics',
addRemoveLinks: true,
autoProcessQueue: false, // this is important as you dont want form to be submitted unless you have clicked the submit button
autoDiscover: false,
paramName: 'pic', // this is optional Like this one will get accessed in php by writing $_FILE['pic'] // if you dont specify it then bydefault it taked 'file' as paramName eg: $_FILE['file']
previewsContainer: '#dropzonePreview', // we specify on which div id we must show the files
clickable: false, // this tells that the dropzone will not be clickable . we have to do it because v dont want the whole form to be clickable
accept: function(file, done) {
console.log("uploaded");
done();
},
error: function(file, msg){
alert(msg);
},
init: function() {
var myDropzone = this;
//now we will submit the form when the button is clicked
$("#sbmtbtn").on('click',function(e) {
e.preventDefault();
myDropzone.processQueue(); // this will submit your form to the specified action path
// after this, your whole form will get submitted with all the inputs + your files and the php code will remain as usual
//REMEMBER you DON'T have to call ajax or anything by yourself, dropzone will take care of that
});
} // init end
};
</script>
控制器代码: -
function addpics() {
$tempFile = $this->request->data['pic']['tmp_name'];
$targetPath = APP.'webroot'.DS.'uploadedpics'.DS;
$targetFile = $targetPath. $this->request->data['pic']['name'];
move_uploaded_file($tempFile,$targetFile);
}
此致
答案 0 :(得分:1)
当您使用dropzone并将表单提交给服务器时,在控制器中,您可以通过使用源获取文件信息,如下所示
$file = $this->params->form['file'];
祝你好运
答案 1 :(得分:0)
APP . DS . 'webroot'.DS.'uploadedpics'.DS
^--Directory Separator might be needet.