Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

431
Visualizações
JavaScript "new Blob" - can you rename the blob's ID?

I have a function that successfully downloads a blob as text; however, its label (or ID, not exactly sure) appears as "blob:file:///*". Is there a way to rename this? blob:file:///

Here's my script:

function download_as_txt(options) {
    let file = new Blob([options.data.toString()], { type: options.type }),
    url = URL.createObjectURL(file),
    a = document.createElement("a")
    ;
    a.href = URL.createObjectURL(file);
    a.download = options.name;
    document.body.appendChild(a);
    
    a.click();

    document.body.removeChild(a);
    window.URL.revokeObjectURL(url);
};

download_as_txt({
    name: `${pages[active_page]["label"]} Export - ${current_epoch}.txt`,
    data: config,
    type: "text",
});

Here's what I've looked into:
• new Blob - option properties appear to allow 'type' and 'endings'
• new File - let file = new File([options.data.toString()], 'test.txt', { type: options.type }), // same as blob, and 'test.txt' doesn't appear anywhere
• troubleshooting - console.log(a) // looks like the name is tied to the 'href' attribute, so it looks like my current "temporary url" method may not work

In the meantime I'll look into using something like fs, but I'd still like to open an explorer window so my users can choose their download directory without needing to fill out a form. ...maybe have them select a directory, download to it, and alert when finished.

Edit:
I made a workaround for Electron:
• dialog.showOpenDialog
• fs.writeFile
• alertify.alert
The code doesn't look as clean as downloading a new blob, but at least I'm not manipulating the DOM (beyond alertify, anyway)

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda