简体   繁体   English

是否可以在Windows-1250的AngularJs JavaScript中保存文件?

[英]Is it possible to Save a file in AngularJs JavaScript in Windows-1250?

I have a webbApp in AngularJs and SpringBoot API REST. 我在AngularJs和SpringBoot API REST中有一个webbApp。
I call a API REST with a GET to retrieve a flow of text-data and try to save it in a file with charset WINDOWS-1250. 我使用GET调用API REST,以检索文本数据流,并尝试将其保存在具有字符集WINDOWS-1250的文件中。

Scenario : 场景:
1) AngularsJs client do a get to the API REST 1)AngularsJs客户端访问API REST
2) API REST read a database, prepare datas and return the result in the body of the response 2)API REST读取数据库,准备数据并在响应正文中返回结果
3) AngularsJS client save the result in a file with charset WINDOWS-1250. 3)AngularsJS客户端将结果保存在具有字符集WINDOWS-1250的文件中。

So, in the third part, i save the file with FileSaver.saveAs but the file is always in UTF-8 Charset. 因此,在第三部分中,我使用FileSaver.saveAs保存了文件,但该文件始终处于UTF-8字符集。 Even if i try to set the charset in Windows-1250. 即使我尝试在Windows-1250中设置字符集。

Someone may help me ? 有人可以帮助我吗?

Angular Test Script: 角度测试脚本:

$http({method:'GET',   
       url: $scope.url + "/cmpcode/" + this.demand.company + "/pcmcode/" + this.demand.pcmcode + "/prlcode/" + this.demand.prlcode +  "/user/" + this.demand.user,  
       responseType : "blob" ,  
       headers:{'X-Auth-Token': sessionService.getLogonDatas.token}})  
.then(function(response) {  
    $scope.showErrorAlert = false;  
    var data = new Blob([response.data], { type: 'text/plain; charset=Windows-1250' });  
    FileSaver.saveAs(data, "MyFile.test");  
    $scope.showSuccessAlert = true;},  
function(rejection) {  
    $scope.showSuccessAlert = false;  
    if (rejection.status === 406){$scope.message.code = "NO_ROWS";}  
    $scope.showErrorAlert = true;});   
};  

SpringBoot Test APIREST: SpringBoot测试APIREST:

public void getPaymentNationalFile(HttpServletResponse response,
                                       @PathVariable("cmpcode") @Length(max = 12) @NotNull String companyCode,
                                       @PathVariable("pcmcode") @Length(max = 12) @NotNull String pcmcode,
                                       @PathVariable("prlcode") @Length(max = 12) @NotNull String prlcode,
                                       @PathVariable("user") @Length(max = 12) @NotNull String user,
{
//Without Try & Catch
PaymentNationalParam paymentNationalParam = new PaymentNationalParam(companyCode, pcmcode, prlcode, user);  
paymentNationalData = paymentNationalService.GetFilePaymentNational(paymentNationalParam);  
response.setContentType("text/plain;charset=" + paymentNationalData.getCharacterEncoding());  //Here "Windows-1250"
int longueur = 0 ;
byte[] buffer = null;
ServletOutputStream servletOutputStream = response.getOutputStream();
for (String string : paymentNationalData.prepareLignePaymentToWrite()) 
{
    buffer = (string + "\n").getBytes() ;
    longueur += buffer.length;
    servletOutputStream.write(buffer , 0, buffer.length);
}
response.setContentLength(longueur);
servletOutputStream.flush();
}

API Response (extract) API响应(摘录)

Header : 标头:
Content-Type : text/plain;charset=windows-1250 内容类型:文本/纯文本;字符集= windows-1250
Transfer-Encoding : chunked 传输编码:分块
X-Content-Type-Options : nosniff X-Content-Type-Options:nosniff

Body extract -> line 1 and 2 of the response (more lines): 110,20170626,2594917,10500086,0,"29105000861000002274422993","16105010701000000101025518","NORAUTO POLSKA SP ZOO|UL. JUBILERSKA 10||04190 WARSZAWA","AUTOLAND 正文提取->响应的第1行和第2行(更多行): 110,20170626,2594917,10500086,0,“ 29105000861000002274422993”,“ 16105010701000000101025518”,“ NORAUTO POLSKA SP ZOO | UL。JUBILERSKA 10 || 04190 WARSZAWA”, “AUTOLAND
43-25|MICKIEWICZA 28| 43-25 | MICKIEWICZA 28 | 0|43-250 PAWĹOWICE",0,10501070,"114024","","","51","" 0 | 43-250PAWĹOWICE“,0,10501070,” 114024“,”“,”,“ 51”,“”

110,20170626,70725,10500086,0,"29105000861000002274422993","58103015080000000502563009","NORAUTO POLSKA SP ZOO|UL. JUBILERSKA 10||04190 WARSZAWA","BOSCH ROBERT SP02-23|JUTRZENKI 105| 0|02-231 WARSZAWA",0,10301508,"114025","","","51","" 110,20170626,70725,10500086,0,“ 29105000861000002274422993”,“ 58103015080000000502563009”,“ NORAUTO POLSKA SP ZOO | UL。JUBILERSKA 10 ||| 04190 WARSZAWA”,“ BOSCH ROBERT SP02-23 | JUTRZENKI 105 | 0 | 02-231 WARSZAWAWA ” 0,10301508, “114025”, “”, “”, “51”, “”

I found a way but i don't know if it's a good. 我找到了一种方法,但我不知道这是否是一种好方法。

So, in the Java part, my response is a octet-stream and put this in Content-type header. 因此,在Java部分中,我的响应是一个八位字节流,并将其放入Content-type标头中。

response.setContentType("application/octet-stream;charset=" + paymentNationalData.getCharacterEncoding());

and in the AngularsJs part, save the file with the Content-type Header 在AngularsJs部分中,使用Content-type Header保存文件

var data = new Blob([response.data],{type: response.headers('Content-Type')}); 
FileSaver.saveAs(data, response.headers('content-filename'));

and work ! 和工作 !

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

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