如何以一种已知格式发布不同格式的文件内容\ struct

问题描述 投票:0回答:1

使用angular8 + node.js-试图弄清楚如何获取(发布)文件内容-不管它是什么格式(在我的情况下是UTF8或base64或二进制)中间件功能,因此以后我可以将其编码(如果需要)为base64 format \ structure。当文件内容为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
1个回答
0
投票

仅通过将文件发布为二进制字符串即可解决。代替 :fileReader.readAsText(event.target.files [0]);

我使用过:

fileReader.readAsBinaryString(event.target.files [0]);

© www.soinside.com 2019 - 2024. All rights reserved.