wasm无法将文件保存到特定目录
Cannot save the file to specific directory by wasm
OS : win10 64位
浏览器:edges 版本 89.0.774.77(官方构建)(64 位)
Emscripten : 1.39.7
函数保存文件
void saveFile(const char *data, size_t length, const char *fileNameHint)
{
// Create file data Blob
val Blob = val::global("Blob");
val contentArray = val::array();
val content = val(typed_memory_view(length, data));
contentArray.call<void>("push", content);
val type = val::object();
type.set("type","application/octet-stream");
val fileBlob = Blob.new_(contentArray, type);
std::cout<<__func__<<": fname hint = "<<fileNameHint;
// Create Blob download link
val document = val::global("document");
val link = document.call<val>("createElement", std::string("a"));
link.set("download", fileNameHint);
val window = val::global("window");
val URL = window["URL"];
link.set("href", URL.call<val>("createObjectURL", fileBlob));
link.set("style", "display:none");
// Programatically click link
val body = document["body"];
body.call<void>("appendChild", link);
link.call<void>("click");
body.call<void>("removeChild", link);
}
我这样称呼它:
auto contents = generate_contents();
saveFile(contents.data(), contents.size(), "C:/users/mypath/01.jpg");
有效,问题是它总是将文件保存到文件名为“c_users_mypath_01.jpg”的下载文件夹中,提示文件是正确的 (C:/users/mypath/01.jpg),但是它总是将文件保存在下载文件夹中,而不是“C:/users/mypath”。
这是浏览器的限制吗?
谢谢
Is this the limitation of the browser?
是的,故意的。您不能只从网页访问用户计算机上的任意路径 - 这将带来巨大的安全风险。
相反,您有以下选项之一:
- 像您现在一样提供文件下载,让用户自行保存/复制/移动到所需目录。
- 将数据存储在 Emscripten 支持的虚拟文件系统之一中,例如IndexedDB - 您可以在此处检查所有选项:https://emscripten.org/docs/api_reference/Filesystem-API.html
- 在最新的 Chrome & Edge 中,使用新的文件系统访问 API,它允许显示例如一个“保存对话框”并让用户选择应该保存文件的目录(尽管与常规下载对话框相比这不会给你太多,并且只会将解决方案限制为基于 Chrome 的浏览器)。 https://web.dev/file-system-access/
OS : win10 64位
浏览器:edges 版本 89.0.774.77(官方构建)(64 位)
Emscripten : 1.39.7
函数保存文件
void saveFile(const char *data, size_t length, const char *fileNameHint)
{
// Create file data Blob
val Blob = val::global("Blob");
val contentArray = val::array();
val content = val(typed_memory_view(length, data));
contentArray.call<void>("push", content);
val type = val::object();
type.set("type","application/octet-stream");
val fileBlob = Blob.new_(contentArray, type);
std::cout<<__func__<<": fname hint = "<<fileNameHint;
// Create Blob download link
val document = val::global("document");
val link = document.call<val>("createElement", std::string("a"));
link.set("download", fileNameHint);
val window = val::global("window");
val URL = window["URL"];
link.set("href", URL.call<val>("createObjectURL", fileBlob));
link.set("style", "display:none");
// Programatically click link
val body = document["body"];
body.call<void>("appendChild", link);
link.call<void>("click");
body.call<void>("removeChild", link);
}
我这样称呼它:
auto contents = generate_contents();
saveFile(contents.data(), contents.size(), "C:/users/mypath/01.jpg");
有效,问题是它总是将文件保存到文件名为“c_users_mypath_01.jpg”的下载文件夹中,提示文件是正确的 (C:/users/mypath/01.jpg),但是它总是将文件保存在下载文件夹中,而不是“C:/users/mypath”。
这是浏览器的限制吗?
谢谢
Is this the limitation of the browser?
是的,故意的。您不能只从网页访问用户计算机上的任意路径 - 这将带来巨大的安全风险。
相反,您有以下选项之一:
- 像您现在一样提供文件下载,让用户自行保存/复制/移动到所需目录。
- 将数据存储在 Emscripten 支持的虚拟文件系统之一中,例如IndexedDB - 您可以在此处检查所有选项:https://emscripten.org/docs/api_reference/Filesystem-API.html
- 在最新的 Chrome & Edge 中,使用新的文件系统访问 API,它允许显示例如一个“保存对话框”并让用户选择应该保存文件的目录(尽管与常规下载对话框相比这不会给你太多,并且只会将解决方案限制为基于 Chrome 的浏览器)。 https://web.dev/file-system-access/