在我将jwt身份验证添加到我的项目之前,文件下载工作正常。我单击下载按钮时遇到此问题:UnauthorizedError:未找到授权令牌。 请采取任何解决方案来避免此问题?
downloadcsv.component.ts:
import { Component, OnInit, Input } from '@angular/core';
import { AdminService } from '../../_services/admin.service';
import {Csv} from '../../_models';
import { Client } from '../../_models';
import {MessageService} from 'primeng/api';
@Component({
selector: 'app-download-csv',
templateUrl: './download-csv.component.html',
styleUrls: ['./download-csv.component.css']
})
export class DownloadCsvComponent implements OnInit {
csvs: Csv[];
ncsv = true;
c: Client[];
csv: Csv;
cl: Client;
constructor(private s: AdminService,private messageService: MessageService) { }
ngOnInit() {
this.s.getAll().subscribe(
data => {this.c = data; });
}
download(fname): string
{
return 'http://localhost:3000/csv/download/' + fname;
}
}
downloadcsv.component.html:
<div class="p-grid p-justify-center" *ngIf="csvs" >
<p-card [style]="{width: '300px'}">
<div *ngFor="let c of csvs" >
{{ c.nomdoc }}
<a href="{{download(c.filename)}}">
<i class="material-icons" style="font-size: 20px;">
get_app
</i></a>
</div>
</p-card>
</div>
csvController.js:
router.get("/download/:filename", function(req, res) {
var file = "./csvuploads/" + req.params.filename;
res.download(file); // Set disposition and send it.
});
router.get("/toto", (req, res) => {
CSV.find((err, docs) => {
if (!err) {
res.send(docs);
} else {
console.log(
"Error in Retriving csvfiles :" + JSON.stringify(err, undefined, 2)
);
}
});
});