【问题标题】:Drupal 7 file upload (without hook_form)Drupal 7 文件上传(没有 hook_form)
【发布时间】:2012-08-15 23:46:46
【问题描述】:

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

另外,不管它到底在做什么,它似乎永久地破坏了看门狗,迫使我重新导入一个新的转储。

【问题讨论】:

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


    【解决方案1】:

    试试这个:

    $('<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"/>
    

    【讨论】:

    • 感谢您的回复。是的,这是一个重要的部分,可能会在某些时候给我带来麻烦,但不幸的是,它并没有解决这个特定的问题。
    • 这样就可以了(缺少 =)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-16
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多