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

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

问题描述

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

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) {
......}

呼叫服务:

service: function (data) {

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

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

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

如果服务具有 Content-Type = "application/x-www-form-urlencoded; charset = utf-8" 或 "application/json; charset = utf-,我会收到 415(不受支持的媒体类型)错误8"或应用程序/表单数据".

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 ".

我正在用 javascript 和 html5 开发前端,我在网上找不到任何可以帮助我的东西,因为 javascript 中不存在 FormDataMultiPart 对象.

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.

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

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

我应该如何格式化要发送到其余调用的数据?

Content-Type 应该如何出现在标题中?

推荐答案

如何使用AngularJS $http发送FormData

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

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"
});

将内容类型标头设置为 undefined 很重要.通常 $http service 将内容类型设置为 application/json.当内容类型为 undefined 时,XHR API 将使用适当的 多部分边界.

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天全站免登陆