【问题标题】:uploadcare fileFrom ngCordova MediaFile从 ngCordova MediaFire 上传护理文件
【发布时间】:2017-05-24 08:49:32
【问题描述】:

我正在尝试将声音文件从 ngCordova 的 $cordovaCapture 服务上传到 UploadCare。 uploadcare.fileFrom('object') 不断失败,并出现“上传”错误。我有公钥集。我可以通过发送文件并标记并访问 document.getElementById('fileTag').files[0] 来上传文件。

$cordovaCapture.captureAudio()
                    .then(function (audioData) {

                        return uploadcare.fileFrom('object', audioData[0])
                            .done(function (fileInfo) {
                                console.log(fileInfo);

                            }).fail(function (err) {
                                console.log(err);

                            })
                    })

audioData[0] 对象如下所示

MediaFile {
    end:0
    fullPath:"file:/storage/emulated/0/Sounds/Voice%20002.m4a"
    lastModified:null
    lastModifiedDate:1481324751000
    localURL:"cdvfile://localhost/sdcard/Sounds/Voice%20002.m4a"
    name:"Voice 002.m4a"
    size:49227
    start:0
    type:"audio/mpeg"
} __proto__:File

我认为问题可能在于该对象是 MediaFile 而不是 File,但我可以使用一些帮助将一个转换为另一个。

FileEntry
    filesystem:FileSystem
    fullPath:"/Sounds/Voice 002.m4a"
    isDirectory:false
    isFile:true
    name:"Voice 002.m4a"
    nativeURL:"file:///storage/emulated/0/Sounds/Voice%20002.m4a"
    __proto__:Entry


File
    end:49227
    lastModified:1481324751000
    lastModifiedDate:1481324751000
    localURL:"cdvfile://localhost/sdcard/Sounds/Voice%20002.m4a"
    name:"Voice 002.m4a"
    size:49227
    start:0
    type:"audio/mpeg"
    __proto__:Object

使用 window.resolveLocalFileSystemUrl() 你最终得到了上面的 FileEntry 对象,它给出了上面的 File 对象,但是 uploadcare 仍然失败并返回“上传”错误。

【问题讨论】:

    标签: javascript angularjs ionic-framework ngcordova uploadcare


    【解决方案1】:

    使用 ngCordova $cordovaFileTransfer() 您可以将音频文件发送到 uploadcare。

            var fileName = filePath.split('/').pop();
            var uploadcareOptions = {
                fileKey: "file",
                fileName: fileName,
                chunkedMode: false,
                mimeType: 'audio/mp4',
                params: {
                    "UPLOADCARE_PUB_KEY": "upload-care-public-key",
                    "UPLOADCARE_STORE": 'auto',
                    fileName: fileName
                }
            };
    return $cordovaFileTransfer.upload('https://upload.uploadcare.com/base/', filePath, uploadcareOptions)
    

    重要的部分是在发送文件时指定 MIME 类型,否则 uploadcare 将假定它是图像。

    【讨论】:

      【解决方案2】:

      uploadcare.fileFrom 从本地文件对象上传文件。试试这个:

      window.resolveLocalFileSystemURL(audioData[0].localURL,function(fileEntry){
      fileEntry.file(function(file) {
          uploadcare.fileFrom('object', file);
          ...
          });
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多