在Meteor上使用Cordova File插件,在getDirectory()回调中接收代码:5 ENCODING_ERROR

时间:2015-06-30 20:07:42

标签: javascript cordova meteor coffeescript cordova-plugins

我在iOS上使用Cordova v4.2.0和Meteor v1.1.0.2。我使用FileTransfer插件(v0.4.3)下载内容(图像,音频等)和文件插件(v1.3.3)来导航文件系统。

当用户首次下载插件并打开应用时,会下载内容。这有效。但是,有时用户需要选择要下载的新批量内容,在这种情况下,我必须再次调用此功能以下载更多内容。第二次,我总是在使用filesystem.root.getDirectory()检索内容目录时收到ENCODING_ERROR。

deferred = Q.defer()
numToLoad = urls.length
numRecieved = 0
urlsToTryAgain = []

onError = (url)->
  return (err)->
    console.log "Error finding file system: "
    console.log err
    deferred.reject(err)

onFileEntrySuccess = (url)->
  return (fileEntry)->
    ft = new FileTransfer()
    endpnt = url.endpointPath()
    uri = encodeURI(endpnt)
    targetPath = fileEntry.toURL()

    ft.onprogress = (event)->
      total = Session.get "total bytes"
      if !total
        total = event.total
        Session.set "total bytes", total
      bytesLoaded = event.loaded
      Session.set "bytes downloaded", bytesLoaded

    onTransferSuccess = (entry)->
      numReceived++
      if numReceived == numToLoad
        deferred.resolve(entry)

    onTransferError = (error)->

     #TIMEOUT_ERROR 
     if error.code == 3
        #try to download the file again
        ft.download(uri, targetPath, onTransferSuccess, onTransferError)
      else
        deferred.reject(error)

    #download the file from the endpoint and save to target path on mobile device
    ft.download(uri, targetPath, onTransferSuccess, onTransferError)


onDirEntrySuccess = (url, directories)->
  return (dirEntry)->
    if directories.length == 0
      file = url.file()
      dirEntry.getFile file, {create: true, exclusive: false}, onFileEntrySuccess(url), onError(file)
    else
      #A branch of the directory tree looks like Content/Image/img.png
      dir = directories[0] + '/'
      remainingDirs = directories.splice(1)
      dirEntry.getDirectory dir, {create: true, exclusive: false}, onDirEntrySuccess(url, remainingDirs), onError(dir)


window.requestFileSystem LocalFileSystem.PERSISTENT, 5*1024*1024, (fs)->
  for url in urls
    #returns an array of the directories, eg ['Content', 'Image']
    directories = url.directories()
    #TODO: this should be done in the object
    firstDir = directories[0] + '/'
    remainingDirs = directories.splice(1)
    #retrieve each directory in the tree one by one, creating it if necessary
    fs.root.getDirectory firstDir, {create: true, exclusive: false}, onDirEntrySuccess(url, remainingDirs), onError(url)
, (err)->
  console.log "ERROR requesting local filesystem: "
  console.log err
  promise.reject err

return deferred.promise

我现在在下载新批次之前删除手机中的所有本地内容,确保下一批的目录清晰。但是,这并没有解决错误。

任何帮助将不胜感激。即使是可能导致ENCODING_ERROR的导致也会有所帮助。

(在使用getDirectory迭代地一次一个目录向下移动树之前,我尝试使用fs.root.getFile和文件目标的完整路径(例如'/Content/Image/img.png')但是,这总是以ABORT_ERROR结束。这种迭代的逐步解决方案解决了这个问题。)

1 个答案:

答案 0 :(得分:0)

第二次我发布了这个问题:p。

我正在启动本地服务器来提供我下载的文件(因为meteor无法访问meteor.local之外的文件)。不知何故,localhost url被附加到第二个wave中每个url的目录数组中,因此它不是['Content','Image']而是['http','127.0.0.1:8080','Content' , '图片']。当然,这导致了ENCODING_ERROR。