如何在Blazor Wasm中读取/导入SQLite数据库?

发布于 2025-02-04 02:40:45 字数 97 浏览 2 评论 0原文

这甚至可行吗?我只能找到如何使其从应用程序中运行,没有什么能接近文件的导入,这将是byte []文件,并使其成为可以实例化的客户端并读/写(在内存)。

Is it even feasible? All I can find is how to get it running from within the app and nothing that approaches the import of the file which will be a byte[] file and make it so that it could be possible to instantiate a client and read/write on it(in memory).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゞ花落谁相伴 2025-02-11 02:40:45

这是可能的,但不是很可行,因为如今您会根据我谦虚的实验添加更多数据/表,从而导致一些奇怪的行为/错误。

首先,您通过HTTP调用将文件读取为流,然后将其转换为基本64字符串,

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),某些或所有支持浏览器中可用。

  • 标题:缓存API不符合HTTP缓存标题。

  • 键匹配键匹配算法取决于变化标头在值中。因此,匹配新密钥需要查看Cache对象中条目的密钥和值。因此,请确保按名称填充库,并仅使用可以安全操作的脚本版本中的录音。这是 mozilla 的一个示例/docs/web/api/service_worker_api/used_service_workers#deleting_old_caches“ rel =” nofollow noreferrer'>删除旧的caches

It's possible but not very feasible, as it would lead to some strange behaviors/errors nowadays as you add more data/tables as per my humble experiments.

First, you read the file as a stream via HTTP call and convert it to a base64 string like so:

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

Then, this is the JavaScript function implementation:

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

Just a few extra notes:

/** Available only in secure contexts. */
declare var caches: CacheStorage; // <-- HTTPS only
declare var crossOriginIsolated: boolean;
declare var crypto: Crypto;
declare var indexedDB: IDBFactory;
// ...
  • Secure context: This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

  • Headers: The caching API doesn't honor HTTP caching headers.

  • key Matching The key matching algorithm depends on the VARY header in the value. So matching a new key requires looking at both key and value for entries in the Cache object. For that reason, make sure to version caches by name and use the caches only from the version of the script that they can safely operate on. Here is an example from Mozilla on how to get rid from old caches via vanilla JavaScript: Deleting old caches

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文