首页 > 解决方案 > Angular 将所有 /(反斜杠)替换为 %5C

问题描述

我正在我的 PC 上构建一个 REST 服务。当我发送请求 url 时,浏览器(我正在使用 chrome)将所有 '/' 转换为 %5C。它适用于某些情况。但大多数情况下 url 都发生了变化。错误:对于 url:http://localhost:3028/controller/addFile?file=C:/Users/Acer/Desktop/Dhana/d/s.txt

GET http://localhost:3028/controller/addFile?file=C:%5CUsers%5CAcer%5CDesktop%5CDhana%5Cd%5Cs.txt 500

我的 component.ts 代码是:

filePath = 'http://localhost:3028/controller/addFile';
   addFile(file) : void{    
    this.params = new HttpParams();
    file = this.directory + '\\' + file;
    this.params = this.params.append('file', file);
    this.http.get(this.filePath, { params: this.params }).subscribe((data: any) => {
});
}

标签: angulartypescript

解决方案


URL 只能使用 ASCII 字符集通过 Internet 发送。

当它包含 ASCII 集之外的字符时,必须将 URL 转换为有效的 ASCII 格式。阅读时必须对 URL 参数进行编码和解码。


推荐阅读