Nativescript图像上载

Nativescript图像上载,nativescript,Nativescript,我正试图上传一张我从相机上拍摄的图像,但它不适用于HTTP模块。后端开发人员说我需要: 要使用的POST方法是“multipart/form”。图像文件将使用 “image”的字段名(这将在PHP中传递给API) $\u文件) 要传入“函数”:“mpostUserAvatar”和“userID”: 123456使用“application/x-www-form_urlencoded”。这会过去的 进入API PHP的$\u帖子 在过去,我使用HTTP模块发布到他们的API,如: const re

我正试图上传一张我从相机上拍摄的图像,但它不适用于HTTP模块。后端开发人员说我需要:

要使用的POST方法是“multipart/form”。图像文件将使用 “image”的字段名(这将在PHP中传递给API) $\u文件)

要传入“函数”:“mpostUserAvatar”和“userID”: 123456使用“application/x-www-form_urlencoded”。这会过去的 进入API PHP的$\u帖子

在过去,我使用HTTP模块发布到他们的API,如:

const requestBody = { 
                    function: 'mpostUserAvatar',
                    userID: this.$store.state.user.id
                }

                this.$http.request({
                    url: this.$apiURL,
                    method: "POST",
                    headers: { "Content-Type": "application/x-www-form-urlencoded"},
                    content: this.$qs.stringify(requestBody)
                }).then((response) => {

                    this.$loader.hide()

                    const agResponse = response.content.toJSON();

                    console.dir( agResponse)

                }, (error) => {
                    console.log(error)
                });
现在,如果我添加我尝试的图像:

const requestBody = { 
                    function: 'mpostUserAvatar',
                    userID: this.$store.state.user.id,
                    image: avatar //this is an image asset instance
                }
还尝试将内容类型更改为:
“内容类型”:“多部分/表单”


如何上载具有
“内容类型”:“多部分/表单”
的文件,以及如何使用
“内容类型”通过参数(
函数
用户ID
)发送文件:“应用程序/x-www-form-urlencoded”
Http模块不支持多部分上载。您必须将图像转换为base64字符串并发送到服务器,或者使用支持多部分上传的插件

更新:

您将在自述文件中找到发送附加参数的示例

var params = [
   { name: "function", value: "mpostUserAvatar" },
   { name: "userID", value: this.$store.state.user.id },
   { name: "image", filename: file, mimeType: "image/jpeg" }
];
var task = session.multipartUpload(params, request);

我有一个解决办法给你。现在,在后端服务器上使用文件流方法可以轻松地使用nativescript后台http上载图像


点击上面的链接,这将帮助您了解nativescript core。

您可以尝试使用后台http模块来查看它是否解决了您的问题吗?@Baskar请参阅我对Manoj帖子的评论。我试过了,但我需要将参数传递给我查看了nativescript后台http,但我没有看到如何传递参数
函数
用户ID
谢谢!我试试看。我在自述中看到了参数,但不知道必须将每个参数作为自己的对象传递