uploadcare文件来自ngCordova MediaFile

时间:2017-01-10 00:05:02

标签: javascript angularjs ionic-framework ngcordova uploadcare

我正在尝试将ngCordova的$ cordovaCapture服务中的声音文件上传到UploadCare。 uploadcare.fileFrom('object')因'upload'错误而一直失败。我有公钥集。我可以通过发送和标记并访问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而不是文件,但我可以使用一些帮助将其中一个转换为另一个。

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 对象,该对象提供了上述文件对象,但uploadcare仍然失败了“上传”错误。

2 个答案:

答案 0 :(得分: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会假设它是图像。

答案 1 :(得分:0)

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

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