[英]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.