有没有办法为urlCreator.createObjectURL(blob)创建文件名

Gab*_*ico 5 javascript

我不太熟悉Javascript,有没有办法创建自己的文件名?发生的情况是它将打开一个新选项卡,并且文件名采用Guid格式。

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);
  }
}
Run Code Online (Sandbox Code Playgroud)

Pie*_*one 1

您可以使用FileSaver.js来实现此目的:

var FileSaver = require('file-saver');
var blob = new Blob(["Hello, world!"], {type: "text/plain;charset=utf-8"});
FileSaver.saveAs(blob, "hello world.txt");
Run Code Online (Sandbox Code Playgroud)