我对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);
}
}
<a>
)并将其download
属性设置为所需的文件名,将其href
设置为您获得的对象URL。但是那样你就无法使用window.open()
。如果你想将文件保存在服务器上,你可以使用一个FormData
对象来发送blob(而不是objectURL!),并将FormData.append()
的第三个参数设置为你想要的文件名。 - Kaiidonew File(blob, fileName)
将 Blob 转换为 File,但在 Chrome 中似乎无法实现。因此,最好的跨浏览器解决方案可能是var win = window.open(URL.createObjectURL(blob)); win.onload = function(){ this.document.title = 'someFile.xxx';};
。请注意,上述任何方法都无法在右键单击保存图像时设置文件名(如果我们谈论的是图像)。 - Kaiido