我如何创建和写入一个新文件(csv),并让浏览器用javascript或D3.js下载它

How do I create and write to a new file (csv) and have the browser download it with javascript or D3.js?

本文关键字:csv 浏览器 下载 js D3 文件 javascript 新文件 创建 何创建 一个      更新时间:2023-09-26

我有一个网站,用户可以在这里查看他们选择的某些数据的图表。我想包括让用户最好在csv文件中获得用于数据的数据的功能。

我已经"准备好"了我的数据,通过用以下格式在数组中排序:

(如果数组打印在控制台中,这只是一个输出示例。)

"CCL5,t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),t(15;17),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),inv(16)/t(16;16),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(8;21),t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,t(11q23)/MLL,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,complex ab. karyotype,HSC,HSC,HSC,HSC,HSC,HSC,MPP,MPP,CMP,CMP,CMP,GMP,GMP,GMP,GMP,GMP,GMP,GMP,MEP,MEP,MEP,MEP,early_PM,early_PM,early_PM,late_PM,late_PM,late_PM,MY,MY,MM,MM,MM,BC,BC,BC,BC,PMN,PMN,PMN,Mono,Mono,Mono,Mono"
"1555759_a_at,6.482143,6.956962,5.842203,6.635454,8.351578,6.719902,5.550186,8.386389,7.195372,8.260547,8.427704,8.822335,6.862085,7.512237,8.985445,8.196036,5.756727,6.386092,6.081735,6.864839,7.182530,7.425995,7.585837,6.726644,5.855473,6.620362,6.911726,6.641145,7.140018,7.239117,5.755348,7.115750,7.254431,7.054359,7.830496,6.757393,7.667946,6.980532,5.902994,7.299082,7.722713,7.419591,9.778600,8.039503,7.263772,6.340856,7.162263,8.067221,7.172460,6.077173,8.991111,5.661114,9.638651,8.878150,7.620753,5.602015,8.488841,8.193242,7.969061,7.168992,8.116956,7.786195,8.487662,7.833534,7.213922,8.085212,7.903602,6.929296,6.390902,7.769445,6.608767,8.722462,7.486884,8.981732,6.629700,6.289574,6.926666,7.012284,7.274458,7.104112,6.763590,7.401222,7.542741,6.751496,6.415097,6.921177,7.244994,6.915105,8.733037,7.775979,6.703853,7.613614,8.361467,7.544937,8.160733,7.902190,7.943476,7.915155,7.819592,8.060850,9.113587,7.236796,7.964187,8.295199,8.086927,7.406500,7.518251,8.621223,8.064387,7.459142,8.791649,9.367919,6.805781,6.579196,7.519587,7.927283,7.656491,8.832018,8.344374,8.195767,8.154316,8.374466,8.628794,8.737380,8.034807,7.471497,6.949001,6.435166,8.450038,6.004613,9.554797,8.290161,9.088501,9.303174,7.160714,9.649978,8.960903,8.642866,9.455499,9.169063,7.765629,8.897696,6.375738,7.506449,8.453261,9.148484,9.557149,8.890534,6.422878,7.631849,8.436778,8.213594,10.05178,8.241344,7.463228,8.457176,9.866300,6.387710,6.452311,7.688277,7.326702,8.879339,10.08454,9.366947,10.48616,7.725666,9.658422,8.225099,6.317467,11.77262,10.42063,8.190103,9.759814,7.090827,11.32362,6.575901,9.104124,10.98599,9.730232,8.675855,8.940791,6.778648,8.616864,9.037348,7.249411,7.615455,11.50930,9.142133,6.268137,9.399649,11.15425,9.707358,9.973163,7.953864,9.598833,8.871507,12.09951,7.277103,6.804820,7.715709,8.564474,6.877368,7.613922,8.524902,8.804725,7.848064,7.961819,9.873879,9.881986,11.22732,7.137895,8.889161,7.516756,8.271140,8.812730,10.82330,7.532594,8.269583,9.449080,9.026222,10.91917,7.645270,9.661588,7.861707,7.452133,9.459665,5.595994,10.53286,8.075182,7.071983,8.122966,7.106455,6.947456,11.30188,7.109542,8.195790,9.763742,7.753196,9.011795,7.345915,9.842569,7.648024,7.459603,8.327268,8.998685,9.126484,8.218383,9.878115,8.470798,8.122966,6.858914,6.945879,4.416836,4.782380,5.313776,5.383624,3.902351,5.534545,5.149230,4.965121,4.778415,4.945832,5.021578,5.617244,5.789978,4.535487,5.062555,5.893881,5.504087,5.004556,4.725728,4.478037,4.639980,4.787633,6.250180,7.080666,6.057337,6.828539,7.824775,6.125071,6.235946,5.060427,4.601436,5.119143,4.844748,5.281926,5.288049,5.297983,5.228851,5.160359,6.178164,5.382761,5.566544,5.839154,5.471390,5.117503"
"204655_at,6.650665,5.961215,4.502137,6.302132,8.173459,5.963574,5.593448,8.654259,7.012104,7.699193,8.975441,9.081770,6.199953,7.092247,9.388275,8.785206,5.236517,6.538903,5.542901,8.381781,7.285328,7.758906,7.306840,7.359977,5.560892,7.241452,6.510188,6.714308,7.363926,7.074580,5.692673,6.403216,6.557037,7.598483,7.064324,7.755842,7.687809,7.235367,5.219403,7.496645,7.190152,6.688152,9.468485,6.991092,5.542230,6.313225,7.614373,7.814979,6.595716,5.921920,8.832513,4.794072,8.976031,8.561321,7.713583,5.508850,8.438366,8.038440,6.839014,6.204068,7.471161,7.434325,8.815445,7.630331,7.144330,6.477527,7.332506,6.962909,7.415869,7.512550,7.016962,7.718584,6.959843,7.841404,6.001597,6.811345,6.037210,7.037965,7.516275,7.061561,6.843173,6.893718,8.027573,6.632176,5.624219,6.251045,7.047511,8.182857,9.332017,8.039766,7.028375,7.593977,8.053367,7.364296,6.966177,8.259199,7.174789,7.286412,7.605856,7.463830,8.965276,7.255868,8.114813,7.822935,7.777868,7.734489,7.768828,9.315155,8.463391,8.207845,8.243579,8.912486,7.023231,6.206887,7.919284,8.257003,6.499889,8.887293,8.399951,7.699658,7.801441,7.360122,8.516605,8.650405,7.336313,7.467414,6.771181,7.332684,8.511542,6.899943,10.21426,8.932474,9.326413,9.339486,7.308780,8.993833,8.908147,8.478899,9.854339,8.860277,7.700106,8.964482,6.538669,7.983135,8.265414,8.855818,9.138643,8.686069,6.718950,7.096758,8.378335,7.929211,9.492477,7.453168,7.287920,9.056026,9.443067,6.707054,6.222710,7.735581,6.969486,9.580289,9.728525,9.546083,10.57794,7.474049,9.345626,8.016472,5.770012,11.34314,10.04684,7.877441,9.610767,6.474897,11.49429,6.416017,8.783111,9.969609,10.65940,8.398946,8.407736,7.083379,9.913488,8.754452,5.849150,7.408506,11.89422,9.441239,6.476942,9.886713,10.85209,9.343652,10.47898,7.218926,9.793217,7.742909,11.66041,7.292561,6.548608,7.414166,8.289258,6.670037,6.940543,8.758918,7.851857,7.345621,7.588678,9.311652,9.574048,10.81287,7.100968,9.032301,7.138636,8.216857,8.676291,11.33970,7.652678,8.236510,9.097110,8.702020,10.82931,7.723108,9.661322,7.465699,9.038610,9.728005,5.556275,10.17158,8.457651,6.387838,8.129716,7.785093,6.856056,11.25984,5.208138,7.205551,9.351631,7.472090,8.560100,6.917197,9.680563,7.077901,7.499877,7.733567,8.627685,9.059433,8.699474,9.963902,8.886752,8.129716,6.572620,6.963223,3.830744,3.604445,3.813575,4.543612,4.582386,4.334254,3.771028,4.288163,4.339538,4.189198,4.439938,5.114137,5.682908,4.753696,5.840181,4.501900,4.011496,5.444103,4.493626,3.734620,4.112952,3.927328,5.163203,5.924691,5.567909,5.666916,5.708866,5.773158,4.988223,4.734517,4.051501,4.140079,3.779642,4.562665,4.596085,4.651792,4.250198,4.965583,4.975683,4.405568,4.474475,4.550982,4.426916,4.310027"
"1405_i_at,4.599676,5.356294,2.902536,5.894694,7.896798,5.683208,3.693357,8.285409,6.479218,8.226262,8.496574,8.220813,6.507729,6.375871,9.043485,7.647009,3.441790,4.460335,5.034970,8.037349,6.815930,6.503717,7.376199,4.771260,3.161119,5.408987,5.475976,5.518551,6.892761,7.515921,3.489281,6.827540,7.093593,6.911564,7.196808,7.427188,5.518241,5.035442,4.881056,6.518617,7.252580,6.626207,9.242115,6.168941,5.576531,5.616965,5.835012,7.034084,6.770410,3.521759,8.863164,2.985059,8.821679,8.546739,6.837130,3.300951,8.410931,7.681839,7.803553,5.734984,7.932826,7.713262,8.308675,7.059521,7.177021,6.499768,5.829755,5.534428,6.280709,6.527007,6.230461,8.042526,6.555088,8.036078,4.118279,5.079335,4.688174,4.612850,5.584097,5.795800,4.134113,5.398835,7.349036,5.759852,3.853642,4.886969,5.713067,7.597313,8.370106,6.654228,5.099130,7.344128,8.364515,7.265915,7.621372,7.695847,6.673858,7.225880,7.402362,7.348774,9.051883,5.992544,6.922981,7.923025,8.193292,6.093729,6.085431,8.930842,7.392228,7.924234,7.223278,7.892058,4.629742,4.336727,6.734394,7.746289,6.431290,8.060350,7.938584,7.795903,8.096038,7.696071,8.195568,8.653560,7.222156,6.293117,5.487318,5.083163,8.042590,6.387247,9.247022,7.990045,8.527196,9.029517,6.715796,9.349496,8.889863,8.625638,9.051151,8.913740,5.724819,8.105797,4.131332,6.676884,8.106372,8.874842,9.412777,8.917826,4.770090,7.573945,8.630658,8.027060,9.606805,7.836757,6.654189,8.547730,9.500316,6.108896,4.098463,6.577948,7.087371,9.141823,9.972382,9.147229,10.31933,7.106247,9.416912,7.494665,5.680458,11.48328,9.820821,7.731811,9.517542,5.894858,10.81487,5.824906,9.071822,10.08832,9.891550,8.635446,8.454205,6.096474,8.708048,8.748310,6.269855,7.622334,11.76967,9.128353,5.539459,9.281204,11.06934,9.028996,9.844261,7.585218,8.943382,8.380330,11.63562,7.270984,6.344478,7.594537,7.174577,4.264520,6.912699,8.157447,8.256793,7.111047,7.281751,9.354995,9.720663,11.16481,7.270010,8.847143,7.062869,8.404660,8.631271,10.81338,7.274801,7.885710,9.211394,9.349408,10.86465,7.690372,9.628380,7.516959,8.251977,9.757341,4.755844,10.45122,8.226947,6.770506,8.270787,7.659480,6.581373,10.80529,5.782499,7.877809,9.431764,7.762795,8.929886,7.146064,9.474732,7.372657,7.121184,7.876557,8.042947,9.059387,8.608525,9.763976,8.881587,8.270787,6.322104,6.920877,1.980104,2.946800,2.315635,2.665076,3.921116,3.290250,3.088347,2.387547,2.423701,2.387166,3.067863,3.928404,4.362160,3.915732,4.722498,3.717603,3.150708,2.870763,2.778725,2.271120,2.208967,2.600295,5.211226,5.749569,5.468895,4.965396,6.107967,5.814134,4.356696,3.833903,2.367238,2.548021,2.412447,2.651955,2.759190,2.892072,2.504659,3.212846,3.796741,2.755456,2.982593,2.922780,3.457164,3.078523"

我如何让用户通过点击按钮下载文件来导出这些数据?我已经创建了一个调用函数的按钮,我只需要知道如何创建文件,写入文件,并让用户下载它

提前非常感谢!

我相信这与这里和这里提出的问题类似。

似乎最直接的解决方案(仅适用于现代浏览器)是使用数据URI。

我分享的第一个问题的答案有一个很好的方法。然后我假设你只需挂上一个按钮或链接就可以启动download功能。