Rij*_*ijo 4 javascript node.js angular nestjs
我有一个使用 Angular 的客户端应用程序,服务器端是 Nest JS。我需要从客户端 -> Nest JS -> Java 端点(第 3 方 API)传递文件数据。
如何将缓冲数据从 Nest JS 服务器传递到 Java 端点
这是我的代码
客户端
let fData: FormData = new FormData;
if(this.formService.model['uploadChecklist']) {
fData.append('clientmanagedFacilityfile',this.formService.model['uploadChecklist']);
fData.append('ticketID', this.ticketId);
fData.append('employeeID', this.eid);
this.gbtService.uploadtoNestJS(fData).subscribe((data) => {console.log(data)})
}
Run Code Online (Sandbox Code Playgroud)
uploadtoNestJS(fData):Observable<any> {
return this.http.post('/api/file/upload/newpoint',fData);
}
Run Code Online (Sandbox Code Playgroud)
Nest JS 服务器端
@Post('file/upload/newpoint')
@UseInterceptors(FileInterceptor('clientmanagedFacilityfile'))
uploadFile(@UploadedFile() clientmanagedFacilityfile, @Headers() headers, @Body() body: {ticketID: string, employeeID: string}) {
console.log(clientmanagedFacilityfile, '--------', headers, '--------', body.employeeID, body.ticketID); // Receiving all the values as expected
return this.fileService.uploadFiletoJava(clientmanagedFacilityfile,body,headers);
}
Run Code Online (Sandbox Code Playgroud)
uploadFiletoJava(files, bodyData: {ticketID: string, employeeID: string} , headers: Headers) {
console.log('----UPLOAD FILE TO JAVA END POINT----');
console.log(files, '------', headers)
const formData = new FormData();
formData.append(files.fieldname, files.buffer, files.originalname);
formData.append('ticketID', bodyData.ticketID);
formData.append('employeeID', bodyData.employeeID)
const baseApiUrl = 'https://api/v1.0.0/sendFileAttachment'
this.httpService.post(baseApiUrl, formData, {headers: headers}).pipe().toPromise();
}
Run Code Online (Sandbox Code Playgroud)
uploadFiletoJava我需要再次转换成formData吗将文件上传到java端点
{
fieldname: 'clientmanagedFacilityfile',
originalname: 'Annamma-cough.pdf',
encoding: '7bit',
mimetype: 'application/pdf',
buffer: <Buffer 25 50 44 46 2d 31 2e 34 0a 25 e2 e3 cf d3 0a 31 20 30 20 6f 62 6a 0a 3c 3c 2f 54 79 70 65 2f 58 4f 62 6a 65 63 74 2f 53 75 62 74 79 70 65 2f 49 6d 61 ... 763882 more bytes>,
size: 763932
} ------ {
'accept-language': 'en-US,en;q=0.9',
'accept-encoding': 'gzip, deflate, br',
referer: 'http://localhost:4200/send',
'sec-fetch-dest': 'empty',
'sec-fetch-mode': 'cors',
'sec-fetch-site': 'same-origin',
origin: 'http://localhost:4200',
'sec-ch-ua-platform': '"Windows"',
'source-id': 'abcder',
'trace-id': '90f3d61e-xysde2-418f-b0e2-af90122621d7',
uuid: '90f3d61e-xysde2-418f-b0e2-af90122621d7',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36',
accept: 'application/json',
'content-type': 'multipart/form-data; boundary=----WebKitFormBoundaryUk4sy5LBUCZm8qkx',
......
}
Run Code Online (Sandbox Code Playgroud)
如果 Node.js 服务器充当代理,并且目标只是传递数据而不进行任何操作,那么它不需要在 Node.js 端解析传入的表单数据/多部分请求。
因此,不需要使用UploadedFile, 和FileInterceptor装饰器。相反,我们需要访问原始请求和响应。
/* or import from http if the target is based on HTTP */
import { request} from 'https';
import { Req, Res } from '@nestjs/common';
import { Request, Response } from 'express';
@Post('file/upload/newpoint')
uploadFile(@Req req: Request, @Res res: Response) {
const proxy = request(
{
hostname: <hostName>,
port: <port>,
method: 'post',
path: <path>,
headers: {
'content-type': req.headers['content-type'],
'content-length': req.headers['content-length'],
// add any other headers as needed
},
},
(resp) => {
// pipe the target(i.e. Java server) response to client
resp.pipe(res);
},
);
// pipe incoming request to the target(i.e. Java server)
req.pipe(proxy);
}
Run Code Online (Sandbox Code Playgroud)
如果您不想将 Java 响应发送到客户端,请删除resp.pipe(res)并添加以下代码片段:
// ...
req.on('end', () => {
res.send(/* a response object */);
});
req.pipe(proxy);
Run Code Online (Sandbox Code Playgroud)