2017-01-10 29 views
0

我想從ngCordova的$ cordovaCapture服務上傳一個聲音文件到UploadCare。 uploadcare.fileFrom('object')保持失敗,出現'上傳'錯誤。我有公鑰集。我可以通過發送文件並標記和訪問document.getElementById('fileTag')。文件[0]來上傳文件。uploadcare文件來自ngCordova MediaFile

$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 

我想這個問題可能是該對象是一個媒體文件,而不是一個文件,但我可以利用一些幫助鑄造一方向另一方。

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()你結束了,讓上面的文件對象,但uploadcare還是失敗,一個「上載」錯誤上述FileEntry的對象。

回答

0

uploadcare.file從本機文件對象上傳文件。試試這個:

window.resolveLocalFileSystemURL(audioData[0].localURL,function(fileEntry){ 
fileEntry.file(function(file) { 
    uploadcare.fileFrom('object', file); 
    ... 
    }); 
}); 
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會認爲它是一個圖像,否則。