Issue With Sending Image Over Ajax
I created a on submit function that grabs different data along with images, if the user has uploaded them and ties the images into a FormData type object. This is than sent along
Solution 1:
You cannot just add a FormData
object as a property to the data
object. You can only send one FormData
object alone, which will get encoded appropriately. If you pass a key-value object, jQuery will try to www-form-urlencode that. See also How to send FormData objects with Ajax-requests in jQuery?.
ElevationColorsForm.prototype.submit = function() {
if (!window.FormData) {
alert("Sorry, your browser does not support uploading files with Ajax.");
throw new Error("no FormData support");
}
function getValue() { return this.value; }
var formdata = new FormData();
var color_name = $(".color-item-input").map(getValue).get().join(", ");
var RestrictionSelectActive = $(".restriction_active_ability option:selected").map(getValue).get();
var color_type = $(".color-item").map(function() {
if ($(this).hasClass('current-color'))
return 'current-color';
if ($(this).hasClass('new_color'))
return 'new-color';
return '';
}).get();
$(".color-item-file").each(function() {
if(this.files.length > 0 && /image.*/.test(this.files[0].type)) {
formdata.append("images[]", this.files[0]);
} else {
formdata.append("images[]", 'none');
}
});
formdata.append('elevation_id', this.Elevation.data.ifpe_id);
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
$.ajax({
url: "actions/save_elevation_colors.php",
type: "post",
data: formdata,
// ^^^^^^^^
processData: false,
contentType: false
}).then(function(data){
console.log(data);
$(".message_box").text("Changes made!");
$(".message_box").fadeIn();
setTimeout(function(){
$(".message_box").fadeOut();
$(".message_box").empty();
},2000);
}, function(){
alert("failure");
});
}
Post a Comment for "Issue With Sending Image Over Ajax"