Setting Content-Type for multipart/form-data

3.4k Views Asked by At

I am trying to upload an image using jQuery (and JAVA) and I keep getting 415 Unsupported Media Type error. The backend code is expecting an Image and a object called "params" to process the image and store it on the server.

The params object contains the coordinates for scaling and cropping the image.

I need to set a Content-Type for each form data, and i don't know how to do that.

my same code is below:

var container = $(document).find(".resize-logo-container");
var containerX = $(container).offset().left;
var containerY = $(container).offset().top;

var image = $(document).find(".resize-logo-image");
var imageWidth = $(image).width();
var imageHeight = $(image).height();

var imageX = $(image).offset().left;
var imageY = $(image).offset().top;

var crop = new Object();
    crop.width = imageWidth;
    crop.height = imageHeight;
    crop.cropWidth = 200;
    crop.cropHeight = 200;
    crop.cropFromX = containerX - imageX;
    crop.cropFromY = containerY - imageY;

var params = JSON.stringify(crop);

var logo = $("#logo-file")[0].files[0];

var data = new FormData();
    data.append("file", logo);
    data.append("params", params);

$.ajax({
    header : {
        "Content-Type" : "multipart/form-data"
    },
    url : "SOME URL",
    data : data,
    type : "POST",
    dataType : "json",
    contentType : "multipart/form-data",
    processData: false,
    success : function(response){

    },
    error : function(){

    }
});

I get:

415 (Unsupported Media Type)

Any pointers/help would be appreciated

Thanks

0

There are 0 best solutions below