简体   繁体   English

通过jquery.form插件上传文件

[英]Uploading files through jquery.form plugin

I've got following html doc for ajax file uploads: 我有以下html doc用于ajax文件上传:

<html> 
<head> 
    <script src="jquery.js" type="text/javascript"></script> 
    <script src="jquery.form.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
       $(document).ready(function() {
           $('#file_upload_form').ajaxForm({
               success: function(data, textStatus, jqxhr) {
                 if (data.redirect)
                    window.location.replace(data.redirect);
                 else
                    alert(data.text);
               },
               error: function(jqXHR, textStatus, errorThrown) {
                  alert("unexpected error: " + textStatus);
               },
               dataType: "json",
           });
       });  
    </script> 
</head> 
<body> 
   <form id="file_upload_form" action="http://apddr/pl/administration/test/" method="post" enctype="multipart/form-data"> 
            <table class="grey" id="file_table"> 
                <tr> 
                    <th colspan="2"> 
                        Add file
                    </th> 
                </tr> 
                <tr> 
                    <td style="vertical-align: top; width: 10em;" class="strong"><label for="id_file">File name:</label></td> 
                    <td> 
                        <input type="file" name="file" size="50" id="id_file" style="background-color: initial;"/><br /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="strong"> 
                        <label for="id_language">Choose lang:</label> 
                    </td> 
                    <td> 
                        <select id="id_language" name="language"> 
                            <option value="">-- no language --</option> 
                            <option value="ANG">english</option> 
                            <option value="NIE">german</option> 
                        </select> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="strong"><label for="id_description">Description:</label></td> 
                    <td> 
                        <input class="text" type="text" name="description" maxlength="300" id="id_description" style="width: 100%;"/><br /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="strong"></td> 
                    <td class="strong" style="text-align: right"> 
                        <input type="submit" class="submit" name="addFile" value="Send" /> 
                    </td> 
                </tr> 
            </table> 
        </form> 
    </body> 
</html>

Calling http://apddr/pl/administration/test/ returns application/json '{'text': 'test'}'. 调用http:// apddr / pl / administration / test /返回application / json'{'text':'test'}'。

Under Chromium alert("unexpected error: " + textStatus); 在Chromium警报下(“意外错误:” + textStatus); line is called with textStatus='aborted' what is unwanted, but firefox is downloading this JSON. 使用textStatus ='aborted'调用该行,这是不需要的,但是firefox正在下载此JSON。 In addition, in firebug console it instantly prints: 此外,在firebug控制台中,它可以立即打印:

[jquery.form] state = uninitialized

What am I doing wrong? 我究竟做错了什么? Help! 救命!

UPDATE: 更新:

OK it appears that transfers are managed by django module - filetransfers (server side), so if I try to upload using filetransfers handler (another action link) It works - but only under Chromium. 好的,看来传输是由Django模块-文件传输(服务器端)管理的,因此,如果我尝试使用文件传输处理程序(另一个操作链接)进行上传,则可以正常工作-但仅在Chromium下使用。 Firefox still crashes Firefox仍然崩溃

UPDATE2: UPDATE2:

It seems that someone crossed this problem: http://crazytechthoughts.blogspot.com/2011/01/get-json-data-response-with-jquery-form.html 似乎有人越过了这个问题: http : //crazytechthoughts.blogspot.com/2011/01/get-json-data-response-with-jquery-form.html

UPDATE3: Solution above works! UPDATE3:上面的解决方案有效!

GitHub solution: never ending loop with blank file input (patch included) GitHub解决方案: 永无止境的空白文件输入循环(包含补丁)

As per malsup comment 根据malsup评论

Added proposed fix to v2.91.

Download jQuery.form.js version 3.50 下载jQuery.form.js版本3.50

Problem solved for me. 问题为我解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM