GoogleChromeLabs / browser-fs-access

File System Access API with legacy fallback in the browser
https://googlechromelabs.github.io/browser-fs-access/demo/
Apache License 2.0
1.38k stars 84 forks source link

Utilizing fileSave with no premade blob. #50

Closed dechowdev closed 3 years ago

dechowdev commented 3 years ago

Hi,

I have tried within a little application to render the window.showSaveFilePicker utilising the fileSave method of the ponyfill. However I cannot seem to circumvent the issue of not being able to provide a blob (even with i utilize new Blob the promise errors)

Are there any tips on how to utilize the fileSave method without passing a blob, as I would like to generate a file on the users machine - in the same manner as it's possible with the Text Editor sample.

https://github.com/GoogleChromeLabs/browser-fs-access/blob/94f651a3d4f7a9220c89c49e5158cefd444cbf9f/src/fs-access/file-save.mjs#L23

dechowdev commented 3 years ago

A quick and easy solution is actually supplying the blob with data, I had wrongly assumed that by generating a blob that would suffice, but the data with an additional mime/type is required. Not easy to remember if you don't fiddle with blobs every day ;)

E.g. this could be a simple text file with a stringified empty JSON object (or text "hello world")

const blob = new Blob(['{}'], { type: 'text/plain'});

Closing as issue is fixed

tomayac commented 3 years ago

Thanks for sharing the solution to your problem. Just noting that you can create an empty file by passing new Blob([''], { type: 'text/plain'}).