0

我正在尝试将声音文件从 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 仍然失败并出现“上传”错误。

4

2 回答 2

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 将假定它是图像。

于 2017-01-28T00:59:28.380 回答
0

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

window.resolveLocalFileSystemURL(audioData[0].localURL,function(fileEntry){
fileEntry.file(function(file) {
    uploadcare.fileFrom('object', file);
    ...
    });
});
于 2017-01-10T20:19:44.477 回答