8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQuery だけで CSVファイル作成とダウンロードを実装する

Last updated at Posted at 2017-05-05

firebase で作ったシステムの管理画面に csvをダウンロードする 機能をつける必要があったので調べて実装したコード。

#環境

#コード
クリックイベント等で下の内容を実装するとjavascriptだけでファイルダウンロードが実装できました。jQuery CSVで作ったCSV文字列を Blob に入れて Aタグのhrefとして処理するコードです。

//CSVダウンロード処理
const serialcode = "ABCDEFG";
const details = [{key:"codeAAAAA",value:"AAAA"},{key:"codeBBBB",value:"BBBB"}];
const content = $.csv.fromObjects(details);
const blob = new Blob([ content ], { "type" : "text/csv" });
$("<a></a>", {href: window.URL.createObjectURL(blob),
              download: serialcode+".csv",
              target: "_blank"})[0].click();
ABCDEFG.csv
key,value
codeAAAAA,AAAA
codeBBBB,BBBB
8
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?