Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to create filename for urlCreator.createObjectURL(blob)

Tags:

javascript

I'm not that familiar with Javascript, is there a way that I can create my own filename? What happens is it opens a new tab and the filename is in Guid Format.

if (urlCreator && headers['content-type'] != 'application/octet-stream') {
  // Fallback to $window.location method
  try {
    // Prepare a blob URL
    // Use application/octet-stream when using $window.location to force download
    var objectUrl = urlCreator.createObjectURL(blob);
    $window.open(objectUrl);
    success = true;
  } catch (ex) {
    //console.log("Download link method with $window.location failed with the following exception:");
    //console.log(ex);
  }
}
like image 734
Gabriel Llorico Avatar asked Nov 09 '22 15:11

Gabriel Llorico


1 Answers

You can use FileSaver.js for this:

var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
like image 94
Pieter Meiresone Avatar answered Nov 15 '22 10:11

Pieter Meiresone