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.37k stars 82 forks source link

`fileSave` does not respect `extensions` #113

Closed rwv closed 2 years ago

rwv commented 2 years ago

Thank you for adding fileSave support (https://github.com/rwv/lookscanned.io/pull/8) for lookscanned.io. However, fileSave does not respect extensions: ['.pdf'] in my setup. Am I doing anything wrong?

Screenshot: image

The meaning of "所有文件" is "All types"

Therefore, when a user change filename, for example, doc. Browser will save file to doc instead of doc.pdf.

Code

await fileSave(blobPromise, {
  fileName: filename,
  extensions: [".pdf"],
  startIn: "downloads",
  id: "lookscanned",
});

Thank you!

tomayac commented 2 years ago

Hmm, it does for me on macOS. What operating system are you on?

Screen Shot 2022-05-30 at 17 16 58
rwv commented 2 years ago

Google Chrome 101.0.4951.67 & Edge 101.0.1210.53 on Windows 11 Version 21H2

rwv commented 2 years ago

On macOS it works for me as well. (Chrome 100.0.4896.127 on macOS Version 10.15.6)

Screen Shot 2022-05-30 at 11 25 17 PM
tomayac commented 2 years ago

Can you try the demo on https://googlechromelabs.github.io/browser-fs-access/demo/ and click "Save Image File" to see if this saves as a .png?

rwv commented 2 years ago

On Chrome 101.0.4951.67 Windows 10 Version 21H2, the dialog has extension in filename but still does not respect the extensions. image

rwv commented 2 years ago

Can you try the demo on https://googlechromelabs.github.io/browser-fs-access/demo/ and click "Save Image File" to see if this saves as a .png?

It works!

image

rwv commented 2 years ago

I tried adding mimeTypes just now and it works.

image

Adobe Acrobat 文档 means Adobe Acrobat Document

Code:

await fileSave(blobPromise, {
  fileName: filename,
  extensions: [".pdf"],
  mimeTypes: ["application/pdf"],
});
tomayac commented 2 years ago

Oh, interesting. It's very weird that it would work for .png but not for .pdf. Thanks for sharing the fix, though.

rwv commented 2 years ago

This happens when mimeTypes is not set and use Promise<Blob>

Promise<Blob> causes type to be null

https://github.com/GoogleChromeLabs/browser-fs-access/blob/617012a85728cb5934c44668652d11b78cfb7256/src/fs-access/file-save.mjs#L34-L45

Therefore, there's no match in the if chain causes types[i].accept == {}

https://github.com/GoogleChromeLabs/browser-fs-access/blob/617012a85728cb5934c44668652d11b78cfb7256/src/fs-access/file-save.mjs#L46-L61

unpervertedkid commented 3 months ago

Oh, interesting. It's very weird that it would work for .png but not for .pdf. Thanks for sharing the fix, though.

I think it does not work for .png. This from my observation and experiments seems to be a windows issue. Even saving an image in excalidraw does not respect the extensions. You can refer to #7860 .

tomayac commented 3 months ago

This from my observation and experiments seems to be a windows issue.

That's correct. It's a known limitation of the fallback API, so it's always recommended to add both, extensions and MIME types.