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

前端 未结 2 397
南笙
南笙 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:32

    in java code you need to write code like this:

    @POST
    @Path("/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {
    

    for more you can refer bellow example and link :-

    https://www.mkyong.com/webservices/jax-rs/file-upload-example-in-jersey/

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题