这是否可行呢?我所能找到的就是如何让它在应用程序中运行,没有任何东西能够接近文件的导入,而文件将是一个byte[]
文件,并使它能够实例化客户机并在其上读取/写入(在内存中)。
发布于 2022-11-25 17:31:48
这是可能的,但不太可行,因为它会导致一些奇怪的行为/错误,因为你现在添加了更多的数据/表,根据我的卑微的实验。
首先,通过HTTP调用将文件作为流读取,并将其转换为如下所示的base64字符串:
var stream = await Http.GetStreamAsync("sqlite.db"); // file in the wwwroot
var buffer = new byte[stream.Length];
using var ms = new MemoryStream(buffer, 0, buffer.Length);
await stream.CopyToAsync(ms);
string base64 = Convert.ToBase64String(ms.ToArray());
await JS!.InvokeVoidAsync("fn.writeToCache", base64);
然后,这是JavaScript函数实现:
window.fn = {
writeToCache: async function (buffer) {
const cachePath = `/data/cache/sqlite.db`;
const cache = await caches.open('your-cache-name');
const resp = await cache.match(cachePath);
if (resp && resp.ok) {
return false;
}
const fileUrl = "data:application/octet-stream;base64," + buffer;
const res = await fetch(fileUrl);
const blob = await res.blob();
const headers = new Headers({
'content-length': blob.size
});
const response = new Response(blob, {
headers
});
await cache.put(cachePath, response);
console.log("Data cached.");
location.Reload(); // Necessary step to be able to see the cache
}
}
只是一些额外的笔记:
/** Available only in secure contexts. */
declare var caches: CacheStorage; // <-- HTTPS only
declare var crossOriginIsolated: boolean;
declare var crypto: Crypto;
declare var indexedDB: IDBFactory;
// ...
https://stackoverflow.com/questions/72493961
复制相似问题