fixed save-as qortal requests

This commit is contained in:
PhilReact 2024-10-25 23:52:37 +03:00
parent 833cecce08
commit 033de5816c
2 changed files with 33 additions and 5 deletions

View File

@ -551,10 +551,34 @@ const testAsync = async (sendResponse)=> {
sendResponse({ result: null, error: "Testing" }); sendResponse({ result: null, error: "Testing" });
} }
const saveFile = (blob, filename) => {
// Create a URL for the blob
const url = URL.createObjectURL(blob);
// Create a link element
const a = document.createElement('a');
a.href = url;
a.download = filename;
// Append the link to the document and trigger a click
document.body.appendChild(a);
a.click();
// Clean up by removing the link and revoking the object URL
document.body.removeChild(a);
URL.revokeObjectURL(url);
};
const showSaveFilePicker = async (data) => { const showSaveFilePicker = async (data) => {
let blob
let fileName
try { try {
const {filename, mimeType, fileHandleOptions, fileId} = data const {filename, mimeType, fileHandleOptions, fileId} = data
const blob = await retrieveFileFromIndexedDB(fileId) blob = await retrieveFileFromIndexedDB(fileId)
fileName = filename
const fileHandle = await window.showSaveFilePicker({ const fileHandle = await window.showSaveFilePicker({
suggestedName: filename, suggestedName: filename,
types: [ types: [
@ -571,7 +595,7 @@ const showSaveFilePicker = async (data) => {
} }
writeFile(fileHandle, blob).then(() => console.log("FILE SAVED")) writeFile(fileHandle, blob).then(() => console.log("FILE SAVED"))
} catch (error) { } catch (error) {
FileSaver.saveAs(blob, filename) saveFile(blob, fileName)
} }
} }

View File

@ -1,5 +1,5 @@
import { useEffect, useState } from 'react'; import { useEffect, useState } from 'react';
import FileSaver from 'file-saver';
class Semaphore { class Semaphore {
constructor(count) { constructor(count) {
this.count = count this.count = count
@ -213,9 +213,13 @@ const UIQortalRequests = [
} }
const showSaveFilePicker = async (data) => { const showSaveFilePicker = async (data) => {
let blob
let fileName
try { try {
const {filename, mimeType, fileHandleOptions, fileId} = data const {filename, mimeType, fileHandleOptions, fileId} = data
const blob = await retrieveFileFromIndexedDB(fileId) blob = await retrieveFileFromIndexedDB(fileId)
fileName = filename
const fileHandle = await window.showSaveFilePicker({ const fileHandle = await window.showSaveFilePicker({
suggestedName: filename, suggestedName: filename,
types: [ types: [
@ -232,7 +236,7 @@ const UIQortalRequests = [
} }
writeFile(fileHandle, blob).then(() => console.log("FILE SAVED")) writeFile(fileHandle, blob).then(() => console.log("FILE SAVED"))
} catch (error) { } catch (error) {
FileSaver.saveAs(blob, filename) FileSaver.saveAs(blob, fileName)
} }
} }