Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.txt extension added on downloaded files on iphone #785

Open
collax opened this issue Jan 10, 2023 · 3 comments
Open

.txt extension added on downloaded files on iphone #785

collax opened this issue Jan 10, 2023 · 3 comments

Comments

@collax
Copy link

collax commented Jan 10, 2023

Hi,

I have a problem that happen only on Iphone, the .txt extension is added at the end of the file when downloaded.
It seem to work fine on other devices (even on safari on mac)

Simple sample code that I use:

    var blob = new Blob([content], {type: "text/plain;charset=utf-8"});
    FileSaver.saveAs(blob, this.workout.name+'.zwo');

For example on Iphone, this will give the file a "workout.zwo.txt" name instead of a "workout.zwo"

Is there a way to fix this ?

Thanks

@jimmywarting
Copy link
Collaborator

hmm, maybe change the type to something like {type: 'application/octet-stream'}?

@snoolord
Copy link

@collax I'm having this exact issue. Have you found a fix?

@snoolord
Copy link

snoolord commented Mar 22, 2023

if anyone is reading this, i figured out a workaround to download the PDF file without the .txt extension from a Blob. (only checked that this works for PDF specifically)
Very hacky, but you need to create a new Blob and specify the type of the Blob to the mimeType: "application/pdf". Maybe there's an enum somewhere that maps file extensions to mimeType and this can be done better, but for now this fixed the issue for me. On desktop, the file downloads like normal and on iPhone it will open the file in the browser and the user can save to their iPhone or iPad.

export const saveFile = async (blob: Blob, suggestedName: string) => {
  const isPdf = suggestedName.includes("pdf");
  if (!isPdf) {
    FileSaver.saveAs(blob, suggestedName);
    return;
  }
  const pdfBlob = new Blob([blob], { type: "application/pdf" });

  const blobUrl = window.URL.createObjectURL(pdfBlob);
  const link = document.createElement("a");
  link.href = blobUrl;
  link.setAttribute("download", `${suggestedName}`);
  document.body.appendChild(link);
  link.click();
  link.parentNode?.removeChild(link);
  window.URL.revokeObjectURL(blobUrl);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants