0

I am trying to upload a sound file from ngCordova's $cordovaCapture service to UploadCare. The uploadcare.fileFrom('object') keeps failing with an'upload' error. I have the public key set. I am able to upload the file by sending it through and tag and accessing 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);

                            })
                    })

the audioData[0] object looks like this

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

I thought the problem might be that the object is a MediaFile rather than a File but I could use some help casting one to the other.

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

using window.resolveLocalFileSystemUrl() you end up with the above FileEntry object that give the above File object but uploadcare still fails with an "upload" error.

Fenix
  • 31
  • 7

2 Answers2

1

Using ngCordova $cordovaFileTransfer() you can send audio files to 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)

The important part is to specify the mime type when sending files as uploadcare will assume it's a image otherwise.

Fenix
  • 31
  • 7
0

uploadcare.fileFrom uploads a file from a native file object. Try this:

window.resolveLocalFileSystemURL(audioData[0].localURL,function(fileEntry){
fileEntry.file(function(file) {
    uploadcare.fileFrom('object', file);
    ...
    });
});
Omar Khazamov
  • 147
  • 1
  • 8