【问题标题】:Nativescript Image UploadNativescript 图片上传
【发布时间】: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" 发送参数(functionuserID

【问题讨论】:

  • 你能试试 background-http 模块看看它是否能解决你的问题吗?
  • @Baskar 查看我对 Manoj 帖子的评论。我试过了,但我也需要传递参数

标签: nativescript


【解决方案1】:

Http 模块不支持分段上传。您必须将图像转换为base64字符串并将其发送到服务器或使用支持多部分上传的nativescript-background-http插件。

更新:

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

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-background-http 但我没有看到如何传递参数 functionuserID
  • 谢谢!我会试试的。我在自述文件中看到了这些参数,但不知道您必须将每个参数作为自己的对象传递
【解决方案2】:

我有一个解决方案给你。现在使用后端服务器上的文件流方法可以很容易地使用 nativescript 背景 http 上传图像。

https://www.ekpotoliberty.com/read/NativeScript-Image-Uploading-to-NodeJs-Api-+-Cloudinary-using-NativeScript-Background-HTTP/5dad52c76dc813685f8da26f

按照上面的链接,这将帮助您了解 nativescript 核心。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-19
    • 2010-12-02
    • 2010-12-15
    相关资源
    最近更新 更多