如何使用AngularJS $ http发送multipart / form-data [英] How to use AngularJS $http to send multipart/form-data

查看:520
本文介绍了如何使用AngularJS $ http发送multipart / form-data的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个使用不同服务的图形界面(用java编写)。
我必须调用这样的服务:

  @PUT 
@Path(nomeServizio )
public Response nomeServizio(final FormDataMultiPart multiPart){
......}

致电服务:

 服务:功能(数据){

返回$ http( {
url:PATH_REST_SERVICES +'/ nomeServizio',
header:{Content-Type:multipart / form-data},
data:data,
method: PUT
});
}

当我从Angularjs服务文件请求时,我得到:错误400(错误请求) )如果服务具有Content-Type = multipart / form-data



如果服务具有Content-Type =我得到415(不支持的媒体类型)错误application / x-www-form-urlencoded; charset = utf-8或application / json; charset = utf-8或application / form-data。



我正在使用javascript和html5开发前端,我没有找到任何可以帮助我的内容,因为在JavaScript中不存在FormDataMultiPart对象。



<我尝试将数据格式化为多种方式发送,但它总是返回400或415.



如何格式化数据以发送到其余部分是吗?



内容类型应该如何插入标题?

解决方案

如何使用AngularJS $ http发送FormData



FormData接口提供了一种轻松构建一组键/值的方法代表表单字段及其值的对,然后可以使用 XHR发送方法。如果编码类型设置为 multipart / form-data ,它使用与表单相同的格式。

  var formData = new FormData(); 

formData.append('type',type);
formData.append('description',description);
formData.append('photo',照片);

返回$ http({
url:PATH_REST_SERVICES +'/ nomeServizio',
header:{Content-Type:undefined},
data:formData,
方法:PUT
});

将内容类型标头设置为 undefined 。通常, $ http服务将内容类型设置为应用/ JSON 。当内容类型为 undefined 时, XHR API 将使用正确的 multipart / form-data 。 com / questions / 3508338 / what-is-the-boundary-in-multipart-form-data>多部分边界


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")
public Response nomeServizio(final FormDataMultiPart multiPart) {
......}

Call service:

service: function (data) {

            return $http({
                url: PATH_REST_SERVICES + '/nomeServizio',
                headers: {"Content-Type": "multipart/form-data"},
                data: data,
                method: "PUT"
            });
        }

When I request from my Angularjs service file I get: error 400 (bad request) if the service has a Content-Type = multipart / form-data

While I get a 415 (unsupported media type) error if the service has a Content-Type = "application / x-www-form-urlencoded; charset = utf-8" or "application / json; charset = utf-8" or "application / form-data ".

I am developing the front-end in javascript and html5, I did not find anything in the web that can help me as the FormDataMultiPart object does not exist in javascript.

I tried to format the data to send in many ways but it always returns 400 or 415.

How should I format the data to send to the rest call?

And how should the Content-Type be in the headers?

解决方案

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.

这篇关于如何使用AngularJS $ http发送multipart / form-data的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆