【发布时间】:2019-10-31 13:29:43
【问题描述】:
使用 angular8 + node.js - 试图弄清楚如何获取(发布)文件内容 - 无论是什么格式(UTF8 或 base64 或二进制,在我的情况下)到节点中间件功能,所以之后我可以将它(如果需要)编码为 base64 格式\结构。 当文件内容为base64时我设法做到了,但每当它是二进制时就会遇到麻烦。
查看我的代码: 客户端: //modalRef.componentInstance 是模态窗口批准上传文件的引用
modalRef.componentInstance.passResult.subscribe(result =>
{
if (result)
{
let fileReader = new FileReader();
fileReader.onload = () =>
{
this.uploadFileContent(fileReader.result.toString());
}
fileReader.readAsText(event.target.files[0]);
//fileReader.readAsText(event.target.files[0], "ascii");
}
event.target.value = null;
this.FileToUpload = null;
});
这是uploadFileContent方法:
uploadFileContent(fileContent : /*string*/any)
{
this.dataService.uploadFile(fileContent).subscribe();
}
这是服务方法:
uploadFile(fileContent : /*string*/any) : Observable<any>
{
let body =
{
file: fileContent
};
return this.http.post<any>(<fileUploadUrl>, body, {
withCredentials:true
}).pipe(
tap(res=>{
console.log('file was uploaded!');
}),
catchError(error => this.doSomething(error))
);
}
服务器端:
router.use(schemaValidation(schema), (req, res, next) =>
{
try
{
let file = req.body.file;
//When receiving the file content in a single fortmat and struct - I would like to encode it
base64 format
【问题讨论】:
标签: node.js angular base64 filereader