2015-09-04 94 views
1

这很简单,我想要做什么!这在Android上正常工作(IOS我可以使用Blob URL)WP8.1将blob写入文件系统

我有一个本地blob,在这种情况下是pdf。我希望用户能够查看PDF。我怎样才能做到这一点?

我已经尝试使用文件插件(org.apache.cordova.file)保存到本地存储,然后使用fileopener2插件来打开它。这种方法适用于Android。对于WP8.1来说,这种方法似乎无效。

第一个问题是我甚至不能将blob保存到本地存储。 Windows Phone 8文档插件的文档非常差,并且使用Google进行搜索的时间很少。

第二个问题是文件插件文档确实将windows phone 8标记为不支持写入blob。为什么是这样?我在代码中注意到blob可以转换为base64编码的字符串,所以我认为这意味着它必须可以在wp8.1中编写一个blob,其中base64编码的字符串被转换回本地部分的blob该插件。

在WP8.1的文件插件中,你使用了什么文件系统URL?没有定义cordova.file。*属性。

如果有人能告诉我如何写一个文本文件到本地存储,然后我可以使用fileopener2插件打开,然后我可以从那里继续。

我很感激任何帮助。非常感谢。

回答

0

您可以编写blob,但您想要保留的所有内容只能保存在本地应用程序存储中。所以只能从已写入文件的应用程序访问文件。

保存的对象序列化/将其转换为一个字符串,并开展这样的事情:

Private Async Function WriteToFileAsync(content As String, folder As String, filename As String) As Task 
    Dim fileBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(content.ToCharArray()) 
    Dim local As StorageFolder = Windows.Storage.ApplicationData.Current.LocalFolder 
    Dim dataFolder = Await local.CreateFolderAsync(folder, CreationCollisionOption.OpenIfExists) 
    Dim file = Await dataFolder.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting) 
    Using stream = Await file.OpenStreamForWriteAsync() 
     stream.Write(fileBytes, 0, fileBytes.Length) 
    End Using 
End Function 

打开现有的PDF文件,使用LaunchFileAsync(...),看看这个页面:How to open pdf file in Windows Phone 8?

您可以使用参数启动其他应用程序,但不幸的是应用程序无法直接相互通信,并且该参数的最大长度不适用于斑点。

0

感谢@anion的回答,这使我找到了解决这个问题的正确途径。

我发现使用fileopener2插件时,我无法将文件(使用标准文件插件)保存到提供的一个路径中,然后让fileopener2再次找到它。诀窍是在fileopener2中添加一个新的方法(仅适用于WP8),在那里我可以传入文件的内容。然后将它保存到本地存储(使用类似于@ anion的答案的代码),然后立即使用类似于现有“打开”方法的代码启动它。

在我的情况下,我已经得到了该文件的base64字符串,然后再将它传递给插件,我猜如果您是以blob开头的话,您会看到类似的文件。

在fileopener2插件,去SRC \ WP8,这增加了FileOpener2.cs:

 public async void saveAndOpen(string options) 
    { 

     string[] args = JSON.JsonHelper.Deserialize<string[]>(options); 
     string aliasCurrentCommandCallbackId = args[2]; 

     try 
     { 
      string base64 = args[0]; 

      byte[] fileBytes = Convert.FromBase64String(base64); 
      StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder; 
      var dataFolder = await local.CreateFolderAsync("SpendControlTemp", CreationCollisionOption.OpenIfExists); 
      var file = await dataFolder.CreateFileAsync(args[1], CreationCollisionOption.ReplaceExisting); 
      using (var stream = await file.OpenStreamForWriteAsync()) 
      { 
       stream.Write(fileBytes, 0, fileBytes.Length); 
      } 


      // Launch the bug query file. 
      await Windows.System.Launcher.LaunchFileAsync(file); 

      DispatchCommandResult(new PluginResult(PluginResult.Status.OK), aliasCurrentCommandCallbackId); 
     } 
     catch (FileNotFoundException) 
     { 
      DispatchCommandResult(new PluginResult(PluginResult.Status.IO_EXCEPTION), aliasCurrentCommandCallbackId); 
     } 
     catch (Exception) 
     { 
      DispatchCommandResult(new PluginResult(PluginResult.Status.ERROR), aliasCurrentCommandCallbackId); 
     } 
    } 

然后,在WWW文件夹,这增加plugins.FileOpener2。js,就在添加“打开”方法的位之前:

FileOpener2.prototype.saveAndOpen = function (base64, fileName, callbackContext) { 
callbackContext = callbackContext || {}; 
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'saveAndOpen', [base64, fileName]); 

};然后,在你的cordova javascript中,你可以做类似这样的事情,但只适用于WP8(你可能可以在合并文件夹中合并这个,所以iOS和Android会这样做):

cordova.plugins.fileOpener2.saveAndOpen(myBase64String, myFileName); 

这让我兴奋起来,希望它能帮助你!