繁体   English   中英

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

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

我在AngularJs和SpringBoot API REST中有一个webbApp。
我使用GET调用API REST,以检索文本数据流,并尝试将其保存在具有字符集WINDOWS-1250的文件中。

场景:
1)AngularsJs客户端访问API REST
2)API REST读取数据库,准备数据并在响应正文中返回结果
3)AngularsJS客户端将结果保存在具有字符集WINDOWS-1250的文件中。

因此,在第三部分中,我使用FileSaver.saveAs保存了文件,但该文件始终处于UTF-8字符集。 即使我尝试在Windows-1250中设置字符集。

有人可以帮助我吗?

角度测试脚本:

$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测试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响应(摘录)

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

正文提取->响应的第1行和第2行(更多行): 110,20170626,2594917,10500086,0,“ 29105000861000002274422993”,“ 16105010701000000101025518”,“ NORAUTO POLSKA SP ZOO | UL。JUBILERSKA 10 || 04190 WARSZAWA”, “AUTOLAND
43-25 | MICKIEWICZA 28 | 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 WARSZAWAWA ” 0,10301508, “114025”, “”, “”, “51”, “”

我找到了一种方法,但我不知道这是否是一种好方法。

因此,在Java部分中,我的响应是一个八位字节流,并将其放入Content-type标头中。

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

在AngularsJs部分中,使用Content-type Header保存文件

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

和工作 !

暂无
暂无

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

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