简体   繁体   English

ajax将多部分表单数据上传为json对象

[英]ajax upload multipart form data as json object

Here there are form data with image. 这里有带图像的表格数据。 I need to send whole image data through json object. 我需要通过json对象发送整个图像数据。 Below I have mentioned the code. 下面我提到了代码。 under the below method I could get whole data except logo. 根据以下方法,我可以获得除徽标之外的所有数据。 I need to send whole data at once 我需要立即发送整个数据

 <form class="form-horizontal" id="companyData">
            <fieldset>

                <legend>Add Company</legend>

                <div class="control-group">
                    <label class="control-label" for="code">Code</label>
                    <div class="controls">
                        <input id="code" name="code" type="text" placeholder="code" class="input-large">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="name">Name</label>
                    <div class="controls">
                        <input id="name" name="name" type="text" placeholder="Name"  class="input-xlarge">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="logo">Logo</label>
                    <div class="controls">
                        <input id="logo" name="logo" class="input-file" type="file">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="username">Admin Username</label>
                    <div class="controls">
                        <input id="username" name="username" type="text" placeholder="" class="input-xlarge">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="AdminPassword">Admin Password</label>
                    <div class="controls">
                        <input id="AdminPassword" name="AdminPassword" type="text" placeholder="" class="input-xlarge">
                    </div>
                </div>

                <div class="control-group">
                    <label class="control-label" for="submit"></label>
                    <div class="controls">
                        <button id="submit" name="submit" class="btn btn-primary">Save</button>
                    </div>
                </div>

            </fieldset>
        </form>

here is my script 这是我的剧本

<script type="text/javascript">
    $(document).ready(function () {
        $("#submit").click(function (e) {
            e.preventDefault();
            var formData = JSON.parse(JSON.stringify(jQuery('#companyData').serializeArray()));
            $.ajax({
                type: "POST",
                url: "",
                data: formData,
                cache: false,
                success: function (data) {
                }
            });

        });
    });
</script>

image is a problem while using json.stringify.Send the formdata object and append all the items into it. 使用json.stringify时,图像是一个问题。发送formdata对象并将所有项目附加到其中。 Try this 试试这个

    var imgFile = document.getElementById('logo');
var imgfileList = imgFile.files;

var formdata = new FormData();
if (imgfileList && imgfileList != null && imgfileList.length > 0) {

    formdata.append(imgfileList[0].name, imgfileList[0]); or add the name
    formdata.append('logofilename', imgfileList[0]);
}
var other_data = $('#companyData :input').serializeArray();
$.each(other_data, function (key, input) {
    formdata.append(input.name, input.value);
});


    $.ajax({
        url: UrlRoot.SaveTeamInfo,
        data: formdata,
        processData: false,
        contentType: false,
        type: 'POST',
        success: function () {

        }
    });

Please remeber to add process data and content type to false as specified in the ajax call above. 请记住将进程数据和内容类型添加为false,如上面的ajax调用中所指定的那样。

try this is formdata is undefined' 试试这是formdata未定义'

if(typeof FormData == "undefined"){
var data = [];
data.push(imgfileList[0].name, imgfileList[0]);
}
else{
var data = new FormData();
    data.append('data', JSON.stringify(inputData));
}

Use .push instead of .append for array 使用.push而不是.append作为数组

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

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