【问题标题】:How to send file buffered data from one serve to another server如何将文件缓冲数据从一个服务器发送到另一台服务器
【发布时间】:2022-01-31 20:08:21
【问题描述】:

我有从客户端收到的以下格式的文件数据,我需要将这些详细信息发送到另一台服务器,但文件没有到达另一端

{
  fieldname: 'clientmanagedFacilityfile',
  originalname: 'Test Word.docx',
  encoding: '7bit',
  mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
  buffer: <Buffer 50 4b 03 04 14 00 06 00 08 00 00 00 21 00 df a4 d2 6c 5a 01 00 00 20 05 00 00 13 00 08 02 5b 43 6f 6e 74 65 6e 74 5f 54 79 70 65 73 5d 2e 78 6d 6c 20 ... 11781 more bytes>,
  size: 11831
} 

我正在使用 NestJS 发送缓冲区数据,这是我的实现。请验证并帮助我

async uploadFiletoIC(files, bodyData: {ticketID: string, employeeID: string} , headers: Headers): Promise<any> {
         console.log('FILES........',files);
        const formData = new FormData(); 
        formData.append(files.fieldname, files.buffer, files.originalname);
        formData.append('ticketID', bodyData.ticketID);
        formData.append('employeeID', bodyData.employeeID);
    
        const request_config = {
            headers: {
            }
        };
        request_config.headers['Authorization'] = headers['authorization'];
        request_config.headers['trace-id'] = headers['trace-id'];
        request_config.headers['uuid'] = headers['uuid'];
        request_config.headers['source-id'] = headers['source-id'];
        request_config.headers['Content-Length'] = headers['content-length'];
        request_config.headers['Accept-Encoding'] = headers['accept-encoding'];
        request_config.headers['Content-Type'] = headers['content-type'];
        request_config.headers['accept'] = '*/*'

        const baseApiUrl = 'https://myurl';
        console.log(request_config);
        const response = await this.httpService.post(baseApiUrl,formData, request_config).toPromise();
        console.log(response.data, 'RESPONSE');
        return Promise.resolve(response.data);
    }  

我需要将缓冲区转换成其他格式吗?


我创建了示例 Nodejs 服务并尝试打印文件,但它不存在于 req 模拟服务器

router.post('/api/file/upload',upload.single('clientmanagedFacilityfile'), function (req, res, next){ 
    console.log(req.file, 'FILESSS*********');
    console.log(req.body, 'REQ BODY######');
    console.log(req, 'REQ');
    return 'success';
})

如果我从邮递员那里调用模拟服务器,文件将正确到达

HTML Angular ts

 if(this.formService.model['uploadChecklist']) {
            let file = this.formService.model['uploadChecklist'] as File;
            fData.append('clientmanagedFacilityfile',file, file.name);
          }
-----
this.gbtService.uploadIC(fData).subscribe((data)=>{console.log(data)})

角度服务

uploadIC(fData):Observable<any> {
        return this.http.post('/api/file/upload/icgbt',fData);
    }

【问题讨论】:

  • 您是否遇到任何错误或接收服务器只是什么也没看到?
  • 服务器 1.,我收到文件数据,但从服务器 1 到服务器到文件丢失 感谢您查看问题
  • 你能把html输入也发一下吗?

标签: node.js angular nestjs


【解决方案1】:

您需要在文件中添加一些额外的字段(例如编码、mimetype 等)。

试试这个:

...
const formData = new FormData(); 
formData.append(files.fieldname, files.buffer, 
 {
   filename: files.originalname, 
   encoding: files.encoding,
   mimeType: files.mimeType 
 }
);
...

这应该适用于单个文件,如果您有多个文件,则对所有文件执行相同的操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-10
    • 2013-12-03
    • 2015-06-25
    • 2019-01-24
    • 1970-01-01
    • 2021-10-22
    • 2018-02-05
    • 1970-01-01
    相关资源
    最近更新 更多