【发布时间】:2019-01-07 15:52:34
【问题描述】:
我正在尝试上传我从相机拍摄的图像,但它不适用于 HTTP 模块。后端开发人员说我需要:
使用的 POST 方法是“multipart/form”。图像文件将使用 “图像”的字段名称(这将被传递到 API PHP 中 $_文件)。
传入“function”:“mpostUserAvatar”和“userID”: 123456 使用“应用程序/x-www-form_urlencoded”。这将通过 进入 API PHP 的 $_POST。
过去我使用 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
}
还尝试将内容类型更改为:"Content-Type": "multipart/form"
如何使用"Content-Type": "multipart/form" 上传文件并使用"Content-Type": "application/x-www-form-urlencoded" 发送参数(function 和userID)
【问题讨论】:
-
你能试试 background-http 模块看看它是否能解决你的问题吗?
-
@Baskar 查看我对 Manoj 帖子的评论。我试过了,但我也需要传递参数
标签: nativescript