【问题标题】:how to post file contents with different formats in a single known format\struct如何以单一已知格式\结构发布不同格式的文件内容
【发布时间】: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


    【解决方案1】:

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

    我用过:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-03-21
      • 2021-10-10
      • 1970-01-01
      • 2021-02-11
      • 2013-03-03
      • 2018-03-10
      • 1970-01-01
      • 2019-03-13
      相关资源
      最近更新 更多