1

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 image is undefined. any idea? I'd like to have here the data sent in my response body.

7
  • 1
    It would be better if you hightlight This is not working and this is expected and then add related code after this:) Commented Jul 25, 2019 at 12:02
  • Have you tried multi-part form data and IFormFile? e.g. Upload(IFormFile file) Commented Jul 25, 2019 at 12:03
  • Btw If I were you, I would first send an image via Postman (or any other help tool). if my backend works, then focus according to result Commented Jul 25, 2019 at 12:04
  • 1
    @MatthiasBurger, nice to hear that (= Then I assume c# and webapi tags are irrelevant right? that's why I asked it in the first place Commented Jul 25, 2019 at 12:10
  • 1
    @ilkerkaran yep, removed them :) Commented Jul 25, 2019 at 12:11

1 Answer 1

2

Map operator always work before subscribe. It provide rewrite http response as you want. You used "map" operator but did not return anything, so subscribe data is going to be empty.

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);
            return response.body; --> you should add this line.
        })
      );
  }
Sign up to request clarification or add additional context in comments.

3 Comments

@nevzatopcu....I guess everytime we don't return value from Map operator?
@MukulSharma if you need to subscribe it and to do something with data you have to. Also you need to subscribe to post and get methods. Only map is not ethic and dont working stable.
@nevzatopcu In the link map does'nt return any thing and works fine- stackblitz.com/edit/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.