简体   繁体   English

使用Blob创建的文件不支持NON-ASCII字符

[英]File created with blob does not support NON-ASCII characters

First of all I have to say that questions similar to this have been asked before, but the answers did not help. 首先,我不得不说,以前已经问过类似的问题,但是答案并没有帮助。

This is the code. 这是代码。

 ctrl.downloadFile = function (file) { var filename = file.filename.split("/"), name = (filename[filename.length - 1]); $http({method: 'GET', url: '/download/' + name}, {responseType: "blob"}).then( function success(response) { var blob = new Blob([response.data], {type: response.headers['content-type'] + ";text/csv;charset=utf-8,%EF%BB%BF"}), url = $window.URL || $window.webkitURL, fileUrl = url.createObjectURL(blob); var anchor = document.createElement('a'); anchor.href = fileUrl; anchor.target = '_blank'; anchor.download = name; anchor.style = "display: none"; document.body.appendChild(anchor); anchor.click(); setTimeout(function () { document.body.removeChild(anchor); }, 100); } ) }; 

This is the html code that calls the downloadFile() function. 这是调用downloadFile()函数的html代码。

 <a ng-click="$ctrl.downLoadFile(file)" target='_blank'>{{file.filename}}</a> 

The Downloaded file does not support German special characters( Å , Ø , Ü ). 下载的文件不支持德语特殊字符( ÅØÜ )。 What to do that the file supports those characters? 该文件支持那些字符怎么办?

Update 1 : I have done this also. 更新1 :我也这样做了。 But it did not work either. 但这也不起作用。

var blob = new Blob(["\ufeff", response.data], 
                     {type: response.headers['content-type'] +
                      ";text/csv;charset=utf-8"})

Update 2 : It works fine if I use these characters( Å , Ø , Ü ) instead of the response.data . 更新2 :如果我使用这些字符( ÅØÜ )代替response.data

   var blob = new Blob(["\ufeff", "Å Ø Ü" ], 
                         {type: response.headers['content-type'] +
                          ";text/csv;charset=utf-8"})

Update 3 : This is a single page application. 更新3 :这是一个单页应用程序。 And Yes ,I have used <meta charset="utf-8"> in the head section of the html page. 是的,我在html页面的顶部使用了<meta charset="utf-8">

Using the whole ctrl.downloadFile() was unnecessary. 不需要使用整个ctrl.downloadFile() Instead, I used a function to make the download link and then call it in ng-href . 相反,我使用了一个函数来制作下载链接,然后在ng-href调用它。

 ctrl.urlForFile = function(file) { var split = file.filename.split("/"); return '/download/' + file['xyz'] + "/" + split[split.length - 1]; }; 

 <a ng-href="{{$ctrl.urlForFile(file)}}" target='_blank'>{{file.filename}}</a> 

If you do not need to make the download link dynamically, you should use the link directly in ng-href . 如果不需要动态创建下载链接,则应直接在ng-href使用该链接。

 <a ng-href="/download/xyz/dfdsf23423423" target='_blank'>{{file.filename}}</a> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM