我想做的事情就是这么简单!这在Android上运行得很好(IOS我可以使用Blob URL)
我有一个本地blob,在这种情况下是一个pdf。我希望用户能够查看pdf。我如何才能做到这一点?
我已经尝试使用文件插件(org.apache.cordova.file)保存到本地存储,然后使用fileopener2插件打开它。这种方法适用于Android。对于WP8.1,这种方法似乎行不通。
第一个问题是我甚至不能将blob保存到本地存储。Windows Phone 8的文件插件的文档非常非常糟糕,谷歌搜索的结果也很少。
第二个问题是文件插件文档确实将windows phone8标记为不支持写入blob。为什么会这样呢?我在代码中注意到blob可以转换为base64编码的字符串,所以我假设这意味着在wp8.1中编写blob是可能的,其中base64编码的字符串在插件的本机部分被转换回blob。
在WP8.1的文件插件中,你使用什么文件系统URL?没有定义cordova.file.*属性。
如果有人能告诉我如何写一个文本文件到本地存储,然后我就可以使用fileopener2插件打开它,然后我就可以从那里开始了。
我很感谢你的帮助。非常感谢。
发布于 2015-09-09 19:35:19
您可以编写blobs,但是您希望持久化的所有内容只能保存在local-app-storage中。因此,文件只能从写入文件的应用程序访问。
要保存一个对象,序列化/将其转换为字符串,并执行类似如下的操作:
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?
您可以使用Parameter启动其他应用程序,但不幸的是,应用程序之间不能直接通信,并且Parameter的最大长度不适用于blobs。
发布于 2015-11-21 00:35:03
感谢@anion的回答,这让我走上了正确的道路,找到了解决这个问题的方法。
我发现,使用fileopener2插件,我无法将文件(使用标准文件插件)保存到提供的路径中,然后让fileopener2再次找到它。诀窍是向fileopener2 (仅用于WP8)添加一个新方法,我可以在其中传递文件的内容。然后它会将其保存到本地存储中(使用类似于@anion的答案的代码),然后使用类似于它现有的"open“方法的代码立即启动它。
在我的例子中,在将文件传递给插件之前,我已经得到了文件的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中,就在添加"open“方法的前面:
FileOpener2.prototype.saveAndOpen = function (base64, fileName, callbackContext) {
callbackContext = callbackContext || {};
exec(callbackContext.success || null, callbackContext.error || null, 'FileOpener2', 'saveAndOpen', [base64, fileName]);};
然后,在你的cordova javascript中,你可以做类似这样的事情,但是只针对WP8 (你也许可以使用merges文件夹将其合并,所以iOS和Android以“正常”的方式进行合并):
cordova.plugins.fileOpener2.saveAndOpen(myBase64String, myFileName);这让我启动并运行起来,希望它能帮助你!
https://stackoverflow.com/questions/32393585
复制相似问题