0

So, i have a system set up to be able to upload files via Angular to NestJS, but i wanna know the progress of the upload, because its a little annoying that i cant see when it finishes.

Is there any way to do it? Thanks!

Heres my code:

FrontEND: Angular:

          let formData = new FormData()

          let dateX = cur_day + hours + minutes + seconds; // removed the vars

          formData.append("dest", `${this.chatid}/${this.userData.userid}/${dateX}/`)
          this.draftImages.forEach((fileData: any) => {


            formData.append("file", fileData.file, fileData.name)
            console.log(fileData.file);
          })

          console.log(formData)
          this.http.post<any>("http://localhost:3000/chat/message/dm/uploadImg", formData).subscribe((result) => {
            console.log(result, formData)
            let filesUploaded: any = []
            this.draftImages = []

            Array.from(result).forEach((file: any) => {
              filesUploaded.push(file.destination + file.originalname)
            })

            this.dmService.sendMessage({
              message: message,
              chatid: this.chatid,
              userid: this.userData.userid,
              username: this.userData.username,
              pfp: this.userData.pfp,
              files: filesUploaded
            });

          })

BackEND: NestJS:

  @Post("uploadImg")
  @UseInterceptors(
    AnyFilesInterceptor({
      storage: diskStorage({
        destination: function (req: any, file, cb) {
          console.log(file);


          function mkdirRecursiveSync(path: string) {
            if (!existsSync(path)) {
              mkdirRecursiveSync(dirname(path));
              mkdirSync(path);
            }
          }

          const destPath = `CDN/attachments/${req.body.dest}`;
          mkdirRecursiveSync(destPath);
          console.log(req.body)
          cb(null, destPath);
        },
        filename: function (req, file, cb) {
          console.log(file)
          cb(null, file.originalname);
        },
      }),
    })
  )
  async uploadedFile(@UploadedFiles() file) {
    console.log(file)
    return file;
  }
4
  • You would need to set up a different type of communication between your server and client other than Request/Response. An option suitable for your use case are Server-Sent events (SSE), or web sockets. There is an integration for NestJs available -> docs.nestjs.com/techniques/server-sent-events Commented Jan 9, 2023 at 11:52
  • i have socket.io set up but i dont know how to upload files with it Commented Jan 10, 2023 at 12:39
  • For clarification, do you want to show the progress bar of the file Upload on Frontend, or do you want the progress of a request processed on the backend? There might be easier solutions, also have a look at this question –> stackoverflow.com/questions/37052623/…. Commented Jan 10, 2023 at 20:18
  • I will try making something from this. Thanks Commented Jan 11, 2023 at 13:05

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.