Wednesday, 19 December 2018

Issue while trying to pass image and its attributes in an array along with form data

About the issue

I am trying to pass image and its attributes in an array along with other form data.

As there are multiple images being posted so that is why posting array of Uploaded Images. Am I doing anything wrong in below question?

This below part is not working and below is the output in php.

enter image description here

var fileData = new FormData();

$.each(Product_Additional_Images, function(index, file) {
    var Product_Sort_No = "1";
    var fileObject = file.files[0];//this is file
    var data = {
        "Image" : fileObject,
        "Sort_No": Product_Sort_No
    }
    fileData.append("Product_Additional_Images[]", JSON.stringify(data));
});

fileData.append('Product_ID', "1");

$.ajax({
    method: "POST",
    url:    "sample url",
    cache:  false,
    async:  true,
    data:   fileData,
    processData: false,
    contentType: false,
    success: function(response) {
    },
    error: function(result) {
    }
});

This one is working perfectly. Below is the one expected output.

This part is working because only array of images along with form data is being posted. Unfortunately, Image attributes in array is not being passed.

enter image description here

var fileData = new FormData();

$.each(Product_Additional_Images, function(index, file) {
    fileData.append("Product_Additional_Images[]", file.files[0]);
});

fileData.append('Product_ID', "1");

$.ajax({
    method: "POST",
    url:    "sample url",
    cache:  false,
    async:  true,
    data:   fileData,
    processData: false,
    contentType: false,
    success: function(response) {
    },
    error: function(result) {
    }
});



from Issue while trying to pass image and its attributes in an array along with form data

No comments:

Post a Comment