发送多部分数据在阿贾克斯的Jquery部分、数据、Jquery、阿贾克斯

2023-09-10 19:46:40 作者:紧张什么

我有我的父窗口弹出有一个表格也。那形式将有一些文件输入类型。现在我想的输入类型的数据与 AJAX 发送。 我曾尝试这样的:

I have a pop-up on my parent window that pop-up have a form also .That form will have some file input type .Now i want to send the data of the input types with the help of ajax. I have tried this :

var formData = new FormData();
formData.append("file", $('#profileImg').get(0).files[0]);

但是,这是不工作的me.Check我的 HTML 的jQuery

HTML

<html xmlns="http://www.w3.org/1999/xhtml">
    <head>

    </head>

    <body>
        <div id="test" style="display:none;box-shadow: 1px 1px 5px; left: 50%; margin: -150px 0 0 -310px;  position: absolute; top: 50%; width: 600px;  z-index: 2;">
            <form name="add_user_form" method="post" enctype="multipart/form-data">
                Name: <input type="text" name="uname" id="uname"/><br/>
                Age:  <input type="text" name="age" id="age"/><br/>
                Profile Image: <input name="profileImg[]" type="file" id="profileImg"/><br />
                Display Image: <input name="displayImg[]" type="file" id="displayImg"/><br />
                <input type="submit" value="Submit" id="addUser"  name="addUser"/>
                <input type="reset" value="Reset" />
            </form>
        </div>

        <div id="list">
            <input type="button" id="addButton" value="Add"/>
        </div>
    </body>
</html>

的jQuery

<script>
            $(document).ready( function() {
                $("#addButton").live('click',function(){
                    $("#test").show();
                });

                $("form[name=add_user_form]").live("submit", function(){
                    var formData = new FormData();
                    formData.append("file", $('#profileImg').get(0).files[0]);

                    alert(JSON.stringify(formData));
                      $.ajax({
                        url: 'bansal.html',
                        type: 'POST',
                        data: formData,
                        async: false,
                        cache: false,
                        //contentType: false,
                        processData: false,
                        success: function (returndata) {
                          alert(JSON.stringify(returndata));
                        }
                      });
                    return false; 
                });
            });

        </script>

建议我如何才能将所有的多部分数据从阿贾克斯

Suggest me how can we send all multipart data from ajax

推荐答案

您可以使用jQuery当作ajaxForm(jquery.form.js)这一点。

You can use Jquery ajaxForm (jquery.form.js) for this.

     $("#yourformid").ajaxForm()
     $("#yourformid").ajaxSubmit(function(response){
              alert('Success');
     });

<form enctype="multipart/form-data" action="Youraction URL">
      <input type="file" name="profileImg"/>
</form>

您可以发送你想尽可能多的文件。

You can send as many files you want.