I'm pretty new to angular and now trying to upload a file and when the file is uploaded successfully it shall create an entry in database with the data that is returned by the api.
for the upload i'm using
import { FileUploader } from 'ng2-file-upload';
This is my function for uploading:
uploadSingleFile() {
if (this.uploader.queue.length <= 0)
return;
const file = this.uploader.queue[0];
const fileItem = file._file;
const data = new FormData();
data.append('file', fileItem);
this.uploadFile(data, () => file.remove()).subscribe((image: any) => {
this.uploadedFile = image;
});
}
uploadFile(data: FormData, onSuccess: any): Observable<any> {
return this.fileService.upload(data, onSuccess, r => { console.error(r); });
}
the fileService looks like:
upload(model: any, onSuccess: any = undefined, onError: any = undefined) {
return this.httpClient
.post(`${this.baseUrl}upload`, model, { observe: 'response' })
.pipe(
map((response: any) => {
console.log(response.status);
if (response.status === 201) { // this works, I'm getting Status 201
if (onSuccess !== undefined)
onSuccess(response);
}
else if (onError !== undefined)
onError(response);
})
);
}
the api function that is called:
[HttpPost, Route("upload")]
public async Task<ActionResult> Upload()
{
// ...
FileForUploadResponseDto fileForUploadDto = new FileForUploadResponseDto
{
FilePath = fileName,
CreationDate = DateTime.Now,
Title = file.FileName,
Size = fileLength
};
return StatusCode(201, fileForUploadDto);
}
It works until this line in uploadSingleFile()
this.uploadFile(data, () => file.remove()).subscribe((image: any) => {
this.uploadedFile = image;
});
the variable
imageis undefined. any idea? I'd like to have here the data sent in my response body.
This is not workingandthis is expectedand then add related code after this:)Upload(IFormFile file)