cakephp - dropzone 与 cakephp 2.5

标签 cakephp dropzone.js

我的问题是我想将 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 页面有表单和 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>

Controller 代码:-

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);
}

问候

最佳答案


当您使用 dropzone 并将表单提交到服务器时,
在 Controller 中您可以通过使用源获取文件信息,如下所示

$file = $this->params->form['file'];

祝你好运

关于cakephp - dropzone 与 cakephp 2.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24695738/

相关文章:

javascript - 在 Ajax 加载的表单中动态初始化 dropzone.js

templates - 如何将文本输入添加到Dropzone上传

mysql - 将另一个 Controller 的另一个字段添加到数组中

cakephp - 如何在 CakePHP 上使用没有数据库的模型并进行关联?

php - CakeDC 用户插件 : How can i install cakedc users plugin, 我已经阅读了文档

javascript - 如何通过将 createImageThumbnails 设置为 false 在 Dropzone JS 中获取图像尺寸?

javascript - 如何使用 dropzone 和 vueJs 上传文件

php - 在 CakePHP 2.x 中使用命名参数

cakephp - jqplot 折线图图例超大

javascript - 使页面上的所有图像都可拖动到特殊的上传字段