Drupal 7文件上传(没有hook_form)

时间:2012-08-15 23:46:47

标签: php javascript post file-upload drupal-7

这是一个让我在过去几天难过的难题。我在Drupal 7中使用模态表单,因此在hook_form系统之外工作,尝试上传图像。表单是通过ajax帖子提交的,这阻止我提交文件和帖子。我所做的是在ajax回调中,使用文件输入创建一个新的表单元素,然后触发提交,发布到我的模块定义的页面。

原始输入元素:

<input type="file" id="chooseImage" name"someImage" class="form-file">

js触发提交:

$.ajax({
type:'POST',
url:$('#originalForm').attr('action'),
data: data,
success: function(response) {
    if (response.success) {
        $('<form id="imageForm" method="POST" action="upload/image/'+response.data.nid+'"></form>').appendTo($('#imageSubmit'));
        $('#chooseImage').appendTo($('#imageForm')); 
        console.log($('#imageForm'));
        $('#imageForm').submit(function(e){
            console.log(e);
            alert('freeze! hammertime...');
        });
        //This should post the file but it isn't...
        $('#imageForm').trigger('submit');
    }
},
dataType:'json'
});

提交事件显示文件属性就好了。但是,在后端,我的页面回调结束...

    function myModule_image_upload($param){
        error_log('number of files = '.sizeof($_FILES));
    }

我没有发布任何文件。我猜测浏览器在.submit()运行后删除帖子中的文件数据,如果是这样的话,我可能无能为力做任何事情,所以我将不得不在其中实现一个单独的菜单用于图像上传的钩子系统。

此外,无论这是真的在做什么,它似乎永久地打破了看门狗,迫使我重新导入新的转储。

1 个答案:

答案 0 :(得分:0)

试试这个:

$('<form id="imageForm" enctype="multipart/form-data" method="POST" action="upload/image/'+response.data.nid+'"></form>').appendTo($('#imageSubmit'));

所以你忘了设置enctype。

另一个错误:

<input type="file" id="chooseImage" name"someImage" class="form-file">

应该是

<input type="file" id="chooseImage" name="someImage" class="form-file"/>