0

这是过去几天让我难过的一个难题。我在 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() 运行之后浏览器正在删除帖子中的文件数据,如果是这种情况,我可能对此无能为力,所以我必须在用于图像上传的钩子系统。

此外,无论这实际上在做什么,它似乎都会永久破坏看门狗,迫使我重新导入一个新的转储。

4

1 回答 1

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"/>
于 2012-08-15T23:50:52.127 回答