使用Android上的二进制字符串通过HTML5保存文件 [英] Save File using Binary String on Android via HTML5

查看:322
本文介绍了使用Android上的二进制字符串通过HTML5保存文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与一个小3人团队与我的应用程序,我们被困。



我们使用JSZip FileSaver 保存我们代码的文件编辑器应用程序。它在浏览器中正常工作,但当我使用它与PhoneGap作为本机Android应用程序它不写。



这个问题仍然发生在zip.js 库,甚至可以使用 FileSaver.js 库。

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



我尝试使用PhoneGap的 FileWriter 对象,但后来我发现它目前不可能用Phonegap的FileWriter编写二进制数据 - 来源 - 来源



但是,这篇文章是几年前。



我也尝试过使用本地WebView并使用 Crosswalk项目仍然没有运气。



我们也试过...

 code> function saveFile(fileName,fileData){
//访问文件系统
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,function(fileSystem){
//创建文件
fileSystem.root.getFile(fileName,{create:true,exclusive:false},function(entry){
//保存文件后,可以使用此URL访问
myFileUrl = entry.toURL()
entry.createWriter(function(writer){
writer.onwriteend = function(evt){
alert(Successfully saved file to+ myFileUrl)
}
//写入文件
writer.write(fileData)
},function(error){
alert(文件编写器+ error.code)
})
},函数(错误){
alert(错误:无法创建文件,+ error.code)
})
},function(evt){
alert(错误:无法访问文件系统,+ evt.target.error.code)
})
}

这个问题是它只保存到用户的内存测试我们所有的Android设备,仍然不工作,但请注意,我们没有使用任何根设备),将不能使用JSZip。



我们的团队开发在许多不同的操作系统(Mac,Android,Chromebook的)上,因此PhoneGap Build目前是一个要求。



有没有任何方式这样做?

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



以下是我们当前使用的PhoneGap Build XML:

 <?xml version =1.0encoding =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>
使用Android上的二进制字符串测试保存文件
< / description>
< author email =mikethedj4@yahoo.comhref =http://mikethedj4.github.io/>
Michael Schwartz
< / author>

< content src =index.html/>

<图标src =icon.pnggap:role =default/>
< icon gap:platform =androidgap:qualifier =ldpisrc =res / icon / android / icon-36-ldpi.png/>
< icon gap:platform =androidgap:qualifier =mdpisrc =res / icon / android / icon-48-mdpi.png/>
< icon gap:platform =androidgap:qualifier =hdpisrc =res / icon / android / icon-72-hdpi.png/>
< icon gap:platform =androidgap:qualifier =xhdpisrc =res / icon / android / icon-96-xhdpi.png/>
< icon gap:platform =androidsrc =res / icon / android / icon-96-xhdpi.png/>
< icon gap:platform =iosheight =57src =res / icon / ios / icon-57.pngwidth =57/>
< icon gap:platform =iosheight =72src =res / icon / ios / icon-72.pngwidth =72/&
< icon gap:platform =iosheight =114src =res / icon / ios / icon-57-2x.pngwidth =114/&
< icon gap:platform =iosheight =144src =res / icon / ios / icon-72-2x.pngwidth =144/&
< icon gap:platform =winphonesrc =res / icon / windows-phone / icon-48.png/>
< icon gap:platform =winphonegap:role =backgroundsrc =res / icon / windows-phone / icon-173-tile.png/>

< preference name =permissionsvalue =none/>
<! - < plugin name =org.crosswalk.enginespec =1.3.0source =pgb/> - >
<! - < plugin name =cordova-plugin-crosswalk-webviewsource =npm/> - >
< plugin name =cordova-plugin-whitelistsource =npm/>
< plugin name =cordova-plugin-devicesource =npm/>
< plugin name =cordova-plugin-filesource =npm/>
< plugin name =cordova-plugin-file-transfersource =npm/>
< plugin name =cordova-plugin-chrome-apps-filesystemsource =npm/>
<! - < plugin name =cordova-connectivity-monitorsource =npm/> - >
< preference name =phonegap-versionvalue =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 = 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'>运行< / button> < textarea class ='form__input'id ='jszipdemo'placeholder ='Demo code here ...'rows ='7'> var zip = new JSZip();&#x000A; zip.file( 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>  

解决方案

您可以使用FileWriter ,这是不可能的,但现在是



以这种方式创建您的blob:

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

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

,然后保存

  fileWriter.write(blob); 


I'm working with a small 3 man team with my app as well and we're stuck.

We're using JSZip and FileSaver to save files for our code editor application. It works fine in the browser, but when I use it with PhoneGap as a native Android application it doesn't write.

This problem still occurs with the zip.js library and even saving single files with the FileSaver.js library.

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

I tried using PhoneGap's FileWriter object, but later I found that it's currently impossible to write binary data with Phonegap's FileWriter - source - source

However that post is a few years old.

I've also tried getting this to work with native WebView and using the Crosswalk Project and still no luck.

We've also tried...

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)
  })
}

The problem with this is it only saves to the users memory (sd card in or not, we've tested on all our android devices and still doesn't work, but note that we are not using any rooted devices), and will not work with JSZip.

Our team develops on many different operating systems (Mac, Android, Chromebook's) so PhoneGap Build is currently a requirement.

Does ANYONE know of any way to do this?
This being referred to Saving a file using a Binary String on Android via HTML5? (PhoneGap or PhoneGap Builds answer is highly preferred)

Here's our current XML for PhoneGap Build:

<?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>

Any help is greatly appreciated.

$('.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>

解决方案

You can save binary data with FileWriter, it was not possible in the past, but now it is

create your blob in this way :

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'});

and then save it

fileWriter.write(blob);

这篇关于使用Android上的二进制字符串通过HTML5保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆