How to use AngularJS $http to send multipart/form-data

前端 未结 2 407
南笙
南笙 2020-12-11 22:59

I am developing a graphical interface that uses different services rest (written in java). I have to call up a service like this:

@PUT
@Path(\"nomeServizio\"         


        
2条回答
  •  时光取名叫无心
    2020-12-11 23:46

    How to use AngularJS $http to send FormData

    The FormData interface provides a way to easily construct a set of key/value pairs representing form fields and their values, which can then be easily sent using the XHR Send method. It uses the same format a form would use if the encoding type were set to multipart/form-data.

    var formData = new FormData();
    
    formData.append('type', type);
    formData.append('description', description);
    formData.append('photo', photo); 
    
    return $http({
        url: PATH_REST_SERVICES + '/nomeServizio',
        headers: {"Content-Type": undefined },
        data: formData,
        method: "PUT"
    });
    

    It is important to set the content type header to undefined. Normally the $http service sets the content type to application/json. When the content type is undefined, the XHR API will automatically set the content type to multipart/form-data with the proper multi-part boundary.

提交回复
热议问题