【问题标题】:Is it possible to Save a file in AngularJs JavaScript in Windows-1250?是否可以在 Windows-1250 的 AngularJs JavaScript 中保存文件?
【发布时间】:2017-07-04 14:21:34
【问题描述】:

我在 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 中设置字符集。

有人可以帮助我吗?

Angular 测试脚本:

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

标题:
内容类型 : text/plain;charset=windows-1250
传输编码:分块
X-Content-Type-Options : nosniff

正文提取物 -> 响应的第 1 行和第 2 行(更多行): 110,20170626,2594917,10500086,0,"29105000861000002274422993","16105010701000000101025518","NORAUTO 波兰 SP Z.O.O|UL。 JUBILERSKA 10||04190 华沙","汽车乐园
43-25|米奇维扎 28| 0|43-250 PAWĹOWICE",0,10501070,"114024","","","51",""

110,20170626,70725,10500086,0,"29105000861000002274422993","58103015080000000502563009","NORAUTO 波兰 SP Z.O.O|UL。 JUBILERSKA 10||04190 华沙","博世罗伯特 SP02-23|JUTRZENKI 105| 0|02-231 华沙",0,10301508,"114025","","","51",""

【问题讨论】:

    标签: javascript angularjs http web-applications


    【解决方案1】:

    我找到了办法,但不知道好不好。

    所以,在 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'));
    

    然后工作!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-29
      • 2021-04-16
      • 2023-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多