angularjs csv下载到本地

Mom*_*ntH 2 angularjs

当我用$ http调用服务时,如果用户单击按钮,我使服务端返回csv格式.

So the return is like 1,2,3,4,5,6,7,8,9,10
Run Code Online (Sandbox Code Playgroud)

为用户提供将本地计算机保存的最佳方法是什么?

wil*_*ill 6

有两种选择

1)从您的服务器发送csv.

2)数据URI.我想这就是你所要求的.这是一个例子

/**
 * @param{string} content The contents of a file to download.
 * @param{string} fileName The name to save the file as on the user's computer.
 */
function(content, fileName) {
  var dataUrl = 'data:text/csv;utf-9,' + encodeURI(content);
  var link = document.createElement('a');
  angular.element(link)
    .attr('href', dataUrl)
    .attr('download', fileName) // Pretty much only works in chrome
  link.click();
}
Run Code Online (Sandbox Code Playgroud)