У меня есть петлевая система с angular, и я пытаюсь подать заявку на файлы и сохранить в указанном каталоге c. У меня результат «204». Однако, как только я позвоню остальным, ничего не произойдет. Вот мой код:
Pobilhete. Js (LoopBack backEnd)
Pobilhete.uploadFile = async (file) => {
const multipartMiddleware = multipart({ uploadDir: './uploads' })
const app = loopback()
app.post('/uploads', multipartMiddleware, (req, res) => {
const files = req.files;
console.log(files)
res.json({ message: files })
})
}
Pobilhete. Json (LoopBack backEnd)
"methods": {
"uploadFile": {
"accepts": [
{
"arg": "ctx",
"type": "object",
"http": {
"source": "context"
}
},
{
"arg": "file",
"type": "Object"
}
],
"returns": {
"type": [
"Pobilhete"
],
"root": true
},
"http": {
"path": "/uploadFile",
"verb": "post"
}
}
}
Service.ts (Angular fronEnd)
uploadFile(file: Set<File>){
const formData = new FormData();
file.forEach(file => formData.append('key', file, file.name));
return this.http.post(`${this.endpointUrl}/uploadFile`, formData)
}
Component.ts (Angular frontEnd)
uploadFile() {
this.bpTnService.uploadFile(this.files).subscribe(data => {
console.log('Finaly')
}, error => {
console.log(error);
});
}
Компонент. html (Angular frontEnd)
<div class="custom-file">
<input type="file" formControlName="archive" class="custom-file-input"
id="inputGroupFile01" (change)="onChangeFile($event)">
<label class="custom-file-label" for="inputGroupFile01" id="fileSelected">Select</label>
</div>
Component.ts Выполнено метод (Angular frontEnd)
private send(page, sort) {
this.vertifyValidators()
if (this.searchForm.invalid) {
this.searchForm.markAllAsTouched();
return;
}
this.response = [];
let searchForm = {
numberbp: this.searchForm.get('numberbp').value,
numberterminal: this.searchForm.get('numberterminal').value,
typefile: this.searchForm.get('typefile').value,
typeOfSearch: this.searchForm.get('typeOfSearch').value,
archive: this.searchForm.get('archive').value,
}
this.bpTnService.findAllTn(searchForm, this.page, this.sort).pipe(
untilDestroyed(this),
).subscribe({
next: (data) => (
this.dataTable.rows = data.content,
this.dataTable.pageable = data.meta,
this.showFilter = false,
this.uploadFile()
),
error: (err) => {
console.error('AgentComponent error', err);
},
complete: () => console.log('AgentComponent stream closed for findAgents'),
});
}
Метод uploadFile () выполняется вместе с send method