如何在window.location.href中指定要下载的csv文件名 [英] how to specify csv file name for downloading in window.location.href

查看:312
本文介绍了如何在window.location.href中指定要下载的csv文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用javascript将数据导出到csv. 由于某些原因,我不允许使用传统 <a download="filename.csv" /a> 设置文件名.

I am exporting data using javascript to csv. for some reason i am not allowed to use the traditional <a download="filename.csv" /a> to set the file name.

我有以下代码行:

window.location.href = "data:text/csv;base64," + csvdata

我在哪里以及如何在其中插入并指定文件名和扩展名使其起作用?

Where and how can i insert and specify the file name and extension to make it work?

推荐答案

不可能那样,尝试通过单击来模拟<a href=..,如下所示:

It's not possible that way, try to emulate the <a href=.. with a click on it like this:

        var csvdata = "Hello World"; //  only for test
        var byteNumbers = new Uint8Array(csvdata.length);

		for (var i = 0; i < csvdata.length; i++)
		{
			byteNumbers[i] = csvdata.charCodeAt(i);
		}
		var blob = new Blob([byteNumbers], {type: "text/csv"});
   
        // Construct the uri
		var uri = URL.createObjectURL(blob);

		// Construct the <a> element
		var link = document.createElement("a");
		link.download = 'myfile.csv';
		link.href = uri;

		document.body.appendChild(link);
		link.click();

		// Cleanup the DOM
		document.body.removeChild(link);
		delete link;

这篇关于如何在window.location.href中指定要下载的csv文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆