通过HTML5在Android上使用二进制字符串保存文件

14

我与一个三人小团队开发应用程序,遇到了问题。

我们使用JSZipFileSaver来保存我们代码编辑器应用的文件。在浏览器中可以正常工作,但是当我将其用于 PhoneGap 作为本地 Android 应用程序时,无法写入。

即使使用 zip.js 库或只保存单个文件的 FileSaver.js 库,这个问题仍然存在。

$('.savemd').click ->
  blob = new Blob([ mdEditor.getValue() ], type: 'text/x-markdown')
  saveAs blob, 'source.md'

我尝试使用PhoneGap的FileWriter对象,但后来我发现目前无法使用Phonegap的FileWriter写入二进制数据-source - source

然而,那篇文章是几年前的了。

我还尝试过使用本机WebView并使用Crosswalk Project,但仍然没有成功。

我们也尝试过...

function saveFile(fileName, fileData) {
  // Get access to the file system
  window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function (fileSystem) {
    // Create the file.
    fileSystem.root.getFile(fileName, { create: true, exclusive: false }, function (entry) {
      // After you save the file, you can access it with this URL
      myFileUrl = entry.toURL()
      entry.createWriter(function(writer) {
        writer.onwriteend = function(evt) {
          alert("Successfully saved file to " + myFileUrl)
        }
        // Write to the file
        writer.write(fileData)
      }, function(error) {
        alert("Error: Could not create file writer, " + error.code)
      })
    }, function(error) {
      alert("Error: Could not create file, " + error.code)
    })
  }, function(evt) {
    alert("Error: Could not access file system, " + evt.target.error.code)
  })
}

这个问题在于它只能保存到用户的内存中(无论是否插入sd卡,我们已经在所有安卓设备上测试过了,但请注意我们没有使用任何root设备),并且不能与JSZip配合使用。

我们的团队在许多不同的操作系统上开发(Mac、Android、Chromebook),因此PhoneGap Build目前是必需品。

有人知道如何做到这一点吗?这指的是通过HTML5在Android上使用二进制字符串保存文件?(最好是PhoneGap或PhoneGap构建的答案)。

这是我们当前的PhoneGap Build XML:

<?xml version="1.0" encoding="utf-8"?>
<widget xmlns = "http://www.w3.org/ns/widgets"
  xmlns:gap   = "http://phonegap.com/ns/1.0"
  id          = "com.save.test"
  version     = "1.0.0"
  versionCode = "1">
  <name>SaveTest</name>
  <description>
      Save file using a Binary string test on Android
  </description>
  <author email="mikethedj4@yahoo.com" href="http://mikethedj4.github.io/">
      Michael Schwartz
  </author>

  <content src="index.html" />

  <icon src="icon.png" gap:role="default" />
  <icon gap:platform="android" gap:qualifier="ldpi" src="res/icon/android/icon-36-ldpi.png" />
  <icon gap:platform="android" gap:qualifier="mdpi" src="res/icon/android/icon-48-mdpi.png" />
  <icon gap:platform="android" gap:qualifier="hdpi" src="res/icon/android/icon-72-hdpi.png" />
  <icon gap:platform="android" gap:qualifier="xhdpi" src="res/icon/android/icon-96-xhdpi.png" />
  <icon gap:platform="android" src="res/icon/android/icon-96-xhdpi.png" />
  <icon gap:platform="ios" height="57" src="res/icon/ios/icon-57.png" width="57" />
  <icon gap:platform="ios" height="72" src="res/icon/ios/icon-72.png" width="72" />
  <icon gap:platform="ios" height="114" src="res/icon/ios/icon-57-2x.png" width="114" />
  <icon gap:platform="ios" height="144" src="res/icon/ios/icon-72-2x.png" width="144" />
  <icon gap:platform="winphone" src="res/icon/windows-phone/icon-48.png" />
  <icon gap:platform="winphone" gap:role="background" src="res/icon/windows-phone/icon-173-tile.png" />

  <preference name="permissions" value="none"/>
  <!-- <plugin name="org.crosswalk.engine" spec="1.3.0" source="pgb" /> -->
  <!-- <plugin name="cordova-plugin-crosswalk-webview" source="npm" /> -->
  <plugin name="cordova-plugin-whitelist" source="npm" />
  <plugin name="cordova-plugin-device" source="npm" />
  <plugin name="cordova-plugin-file" source="npm" />
  <plugin name="cordova-plugin-file-transfer" source="npm" />
  <plugin name="cordova-plugin-chrome-apps-filesystem" source="npm" />
  <!-- <plugin name="cordova-connectivity-monitor" source="npm" /> -->
  <preference name="phonegap-version" value="cli-5.2.0" />

  <allow-intent href="http://*/*" />
  <allow-intent href="https://*/*" />
</widget>

$('.download').click(function () {
  eval($('#jszipdemo').val())
})
.fill {
  width: 100%;
  display: block;
}
<link rel='stylesheet prefetch' href='http://treehouse-code-samples.s3.amazonaws.com/poly/css/application.css'>

<div class='grid'>
  <div class='grid__col--12'>
    <p></p>
    <button class='btn--default download fill'>Run</button>
    <textarea class='form__input' id='jszipdemo' placeholder='Demo code here...' rows='7'>var zip = new JSZip();&#x000A;zip.file("Hello.txt", "Hello World");&#x000A;var folder = zip.folder("images");&#x000A;folder.file("folder.txt", "I'm a file in a new folder");&#x000A;var content = zip.generate({type:"blob"});&#x000A;// see FileSaver.js&#x000A;saveAs(content, "example.zip");</textarea>
  </div>
</div>

<script src='https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js'></script>
<script src='https://stuk.github.io/jszip/dist/jszip.js'></script>
<script src='https://stuk.github.io/jszip-utils/dist/jszip-utils.js'></script>
<script src='https://stuk.github.io/jszip/vendor/FileSaver.js'></script>


你可以尝试使用我的下载器,但它可能与FileSaver的支持范围相同。它将被用作上面的download(content, "example.zip");。如果这不起作用,那么你的Android版本可能不支持其中之一:a[download]Blob()window.URL,这很可能是一个致命问题。不过,在download.js中我尝试了几个备选方案,所以值得一试,即使成功的机会很小。 - dandavis
如果您的saveFile()函数将文件保存到SD卡或设备上,它是否能正常工作? - whodeee
在 Android 4.4 以下的版本,你需要 SD 卡才能下载任何东西,我认为这可能是一个供应商设定,但似乎是一个常见的模式。 - dandavis
1个回答

4

您可以使用FileWriter保存二进制数据,这在过去是不可能的,但现在可以实现。

按照以下方式创建您的blob:

var byteArray = new Uint8Array(fileContent.length);
for (var x = 0; x < byteArray.length; x++) {
    byteArray[x] = fileContent.charCodeAt(x) & 0xff
}

var blob = new Blob([byteArray], {
    type: 'application/octet-stream'
});

然后保存它

fileWriter.write(blob);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接