#How to save an image from a blob URL? (SolidJS and TypeScript)
14 messages · Page 1 of 1 (latest)
Do you mean a copy that you save to disk? Can't you just use tauri's writeFile api with blob.arrayBuffer() or blob.bytes() or blob.stream() ? (stream() requires the very latest versions of the fs plugin)
So I’m doing that, but I currently have a different problem now. It doesn’t seem like mkdir is working.
const imagesDir = `images`;
const deckDir = deckName;
const filePath = `${imagesDir}/${fileName}`;
// Check and create directory if not exists
const dirExists = await fs.exists(imagesDir);
if (!dirExists) {
await fs.mkdir(imagesDir, { baseDir: fs.BaseDirectory.AppLocalData });
}
I had it print the error, and it says "forbidden path: images"
These are my permissions:
"permissions": [
"core:default",
"shell:allow-open",
"fs:default",
"shell:default",
"dialog:default",
"dialog:default"
]
you also need to configure scopes unless you use the dialog to select the target dir (it will extend the scope at runtime automatically) https://v2.tauri.app/plugin/file-system/#scopes
So I did that, and it's still not working for some reason. It's still giving me the same error.
"identifier": "migrated",
"description": "permissions that were migrated from v1",
"local": true,
"windows": [
"main"
],
"permissions": [
"core:default",
"shell:allow-open",
"fs:default",
"fs:allow-open",
"fs:allow-write",
"fs:allow-read",
"fs:allow-rename",
"fs:allow-mkdir",
"fs:allow-remove",
"fs:allow-write-text-file",
"fs:scope-download-recursive",
"fs:scope-resource-recursive",
{
"identifier": "fs:scope",
"allow": [{ "path": "$APPLOCALDATA" }]
},
"shell:default",
"dialog:default"
]
}```
I also did some research and added the setup to my rust main code, but it's still not working..
``` tauri::Builder::default()
.setup(|app| {
let path = app.path().app_data_dir().expect("This should never be None");
let path = path.join(".images/");
create_dir_all(&path);
Ok(())
})
.plugin(tauri_plugin_dialog::init())
.plugin(tauri_plugin_shell::init())
.plugin(tauri_plugin_fs::init())
.invoke_handler(tauri::generate_handler![
/*REDACTED*/
])
.on_window_event(|window, event| {
if let WindowEvent::CloseRequested { .. } = event {
// Perform the save action here
if let Err(e) = save_to_disk() {
eprintln!("Failed to save data to disk: {}", e);
}
}
})
.run(tauri::generate_context!())
.expect("error while running tauri application");
you only allowed the applocaldata folder itself but not its contents, try this json { "identifier": "fs:scope", "allow": [{ "path": "$APPLOCALDATA" }, { "path": "$APPLOCALDATA/**" }] }, (maybe has to be **/* - the behavior changed in some places compared to v1)
So I tried $APPLOCALDATA/**, $APPLOCALDATA/*, and $APPLOCALDATA/**/* in my migrated.json file, but none of those seemed to work. Do you know anything else that might be a cause of it?
Updated migrated.json file:
{
"identifier": "migrated",
"description": "permissions that were migrated from v1",
"local": true,
"windows": [
"main"
],
"permissions": [
"core:default",
"shell:allow-open",
"fs:default",
"fs:allow-open",
"fs:allow-write",
"fs:allow-read",
"fs:allow-rename",
"fs:allow-mkdir",
"fs:allow-remove",
"fs:allow-write-text-file",
"fs:scope-download-recursive",
"fs:scope-resource-recursive",
{
"identifier": "fs:scope",
"allow": [{ "path": "$APPLOCALDATA" }, {"path": "$APPLOCALDATA/*" }]
},
"shell:default",
"dialog:default"
]
}
So I figured this out, and it's saving the file now, thankfully! However, the image does not seem to be displaying. Would you happen to know how to display the image from the AppData folder?
URL setting:
const appDataPath = await appDataDir();
const localPath = `images/New Deck-0.jpeg`; // Relative path from AppData
const fullPath = appDataPath + "/" + localPath;
setFileURL(fullPath.replace(/\\/g, "/"));```
img tag:
``` <img ref={(el) => (imageRef = el)} src={fileURL()} style="max-width: 100%; position: absolute; top: 0; left: 0;" onLoad={onImageLoad}/>```
what's the full img.src / fileURL() now?