javascript - 使用带有 Meteor 的 Cordova 文件插件,收到代码 : 5 ENCODING_ERROR in getDirectory() callback

标签 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 结束。这种迭代逐步解决方案解决了该问题。)

最佳答案

我发布这个问题的时候就解决了:p。

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

关于javascript - 使用带有 Meteor 的 Cordova 文件插件,收到代码 : 5 ENCODING_ERROR in getDirectory() callback,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31147548/

相关文章:

cordova - 我的phonegap应用程序中出现"No Content-Security-Policy meta tag found."错误

android - Android设备上隐藏了滚动条,如何显示垂直滚动条?

javascript - Meteor.js 和 Collection2 : Update Method that Inserts New Object in subschema

coffeescript - Meteor:如何从模板助手返回模板?

javascript - 无法在 typeahead js 中设置与 displaykey 不同的 valuekey

javascript - 根据 ng-repeat 的数据更改 Angular Directive(指令)元素的 CSS(或类)

javascript - 在 JavaScript 中调用 WebService

javascript - 是否可以使用 Lua/Javascript 脚本使用新变量扩展 C# 对象?

css - 温泉主题不适用

javascript - 请求 Meteor + autoform 示例