【问题标题】:Blobstore upload with javascript使用 javascript 上传 Blobstore
【发布时间】:2011-09-19 17:04:14
【问题描述】:

这是 HTML 的最小声明,以便在 upload_url 中的 Blobstore 中上传文件。此解决方案需要点击提交按钮才能提交并重定向内容。如何在不丢失 enctype 的情况下使用 javascript 或 jQuery 在后台发帖?

<form id="upload_file" action="{{upload_url}}" enctype="multipart/form-data" method="post">
    <input type="file" name="file">
    <input type="submit" name="submit" value="Submit">
</form>

【问题讨论】:

    标签: javascript python html google-app-engine blobstore


    【解决方案1】:

    jQuery Form plugin 允许您使用 Ajax 在后台提交多部分表单。

    例子:

    $('#upload_file').submit(function() { 
        var options = { 
            clearForm: true        // clear all form fields after successful submit 
        }; 
        $(this).ajaxSubmit(options);
        return false; 
    });
    
    $('[name=submit]').click(function(){
        $('#upload_file').submit();        
    });
    

    静默完成这项工作需要您将“提交”输入替换为“按钮”输入:

    <form id="upload_file" action="{{upload_url}}" enctype="multipart/form-data" method="post">
            <input type="file" name="file">
            <input type="button" name="submit" value="Submit">
    </form>
    

    【讨论】:

    • P 我有这个工作,但我想避免的是点击提交按钮。由于我选择了我希望自动提交的文件 - 无需点击任何按钮即可更新。
    • 试试这个:$('[name=file]').change(function(){ $('#upload_file').submit(); });
    • 运气不好,当我选择文件时它看起来不像触发 .change()
    • 您使用的是什么版本的 jQuery?看起来 .change 触发器只能在 1.4.2 或更高版本的文件输入上正常工作。看看这个related question
    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 2012-03-05
    • 2016-10-29
    • 1970-01-01
    • 2011-11-03
    • 2015-06-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多