感谢@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);
这让我兴奋起来,希望它能帮助你!