【问题标题】:can't upload images via FileTransfer plugin of ionic 3 to Cloudinary无法通过 ionic 3 的 FileTransfer 插件将图像上传到 Cloudinary
【发布时间】:2019-07-20 23:27:51
【问题描述】:

我正在尝试将图像从移动图库上传到 cloudinary。 我遵循了我在this link 中找到的将照片从角度上传到云端的文档中的逻辑。

我使用File Transfer插件将照片从ionic3应用上传到cloudinary。

这是upload() 函数:

upload() {
    const fileTransfer: FileTransferObject = this.transfer.create();
    let options: FileUploadOptions = {
        fileKey: 'testName',
        fileName: 'test',
        mimeType : "text/plain",
        headers: { 'Content-Type': 'application/json', 'X-Requested-With': 'XMLHttpRequest' },
        params:{upload_preset:'preset'}
    };
    let url = 'https://api.cloudinary.com/v1_1/<cloud_name>/image/upload';
    if (this.imageURI) {
        fileTransfer.upload(this.imageURI[0], encodeURI(url), options)
                .then((data) => {
                    console.log('uploaded heeere data', JSON.stringify(data))
                }, (err) => {
                    console.log('errorrrr', JSON.stringify(err))
                })
    }
}

this.imageUri[0]是一张以64为底的照片

app.module.ts 中我指定了cloudinary 配置:

 import { CloudinaryConfiguration, CloudinaryModule } from "@cloudinary/angular-5.x";
 import { Cloudinary } from "cloudinary-core";


@NgModule({
declarations: [....],
imports: [
BrowserModule,
HttpClientModule,
CloudinaryModule.forRoot({Cloudinary}, {
      cloud_name: "<cloud_name>",
      upload_preset: 'preset',
      api_key:'<api_key>'
  } as CloudinaryConfiguration),
]
.......
})

但我上传失败。 没有显示错误.. 只是一条消息:File Name is too long

我不知道连接是否建立,是cloudinary拒绝长名称的文件还是什么?

有人可以帮我调试一下,看看问题出在哪里吗?或者有人可以帮我解决这个问题吗?

非常感谢。

【问题讨论】:

    标签: ionic-framework file-upload ionic3 file-transfer cloudinary


    【解决方案1】:

    你应该发送图像名而不是base64编码,服务器读取base64作为名称文件,所以你应该在第一个参数中发送文件名

    【讨论】:

    • 但是我在options 中设置了`fileName: 'test'` .. 不正确吗?
    • 不,您没有为示例定义扩展名:test.png,同样在第一个参数中,您应该传递图像路径而不是 base64,如您在文档中看到的那样:fileTransfer.upload('', '',选项)
    猜你喜欢
    • 2015-12-25
    • 2018-06-11
    • 2014-01-12
    • 2020-08-10
    • 2021-09-03
    • 1970-01-01
    • 2021-03-20
    • 2020-01-15
    • 2020-05-02
    相关资源
    最近更新 更多