2014-06-17 31 views
0

我正在开发一个使用手机差距的应用程序。我捕捉图像/音频/视频并将其路径存储在本地存储中。我需要获取捕获文件的base64并将其存储在数据库中,然后将其与服务器同步。这可能使用JavaScript?如何使用JavaScript将捕获的媒体文件转换为base64?

我尝试使用FileReader API,即电话差距提供,但函数reader.onloadend()没有得到执行。

可以设法使用画布获取图像的base64,但使用画布可以获得音频和视频的base64吗?

谢谢。

+0

你需要使用multipart WS因为你的音频/视频base64非常大你 –

+0

你能解释一下吗?对于应用程序,最初它会将所有数据存储在本地存储中,一旦用户同步数据,它将被上传到服务器上。所以,我需要在本地存储捕获文件的base64。 – gajjarkruti

回答

0

是有可能得到媒体文件的base64 reader.onloadend()没有被调用,因为你可能错过了这一行reader.readAsDataURL(file)。

  // here i have a file input and i am using the FileReader Api to read the content on it's change event (i have no idea what is the event for capturing a media in phonegap 
$("#file").change(function(e){ 
      var file = e.currentTarget.files[0]; 

      var FR = new FileReader(); 

      FR.onload = function (encodedFile) { 
       //alert("onload is called."); 
      } 

      FR.onloadend = function (encodedFile) { 
       var src = encodedFile.target.result; 
       src = src.split("base64,"); 

       // This is the base64 encoded string of that media file which you will be interested to store in the database(post to the server 
       var contentAsBase64EncodedString = src[1]; 
      } 
      reader.readAsDataURL(file); 

    } 
+0

我已经以相同的方式实现它,但reader.onloadend()不执行。下面是我的代码函数gotFile(文件){ \t \t readDataUrl(file); \t \t \t } \t \t \t功能readDataUrl(文件){ \t \t变种读者=新的FileReader();阅读器功能(evt){ \t \t console.log(evt.target.result); \t \t}; \t \t data = reader.readAsDataURL(file); \t \t WL.Logger.debug(data); \t \t} – gajjarkruti

+0

@gajjarkruti也许它崩溃,所以请添加reader.onerror = function(){alert(“error”);}来检查它。 –

相关问题