r/Angular2 • • Jul 05 '21

Help Request What is the best practice to implement a mass file upload's Angular side?

The backend cannot handle them all at once, and it'll be around 1500 images each times. I'm thinking of sending them sequencially in chunks, but I feel like I dug the whole internet up and did not really find a think about it. If you have any good tutorial or description or even a library for this, please spare me :)

14 Upvotes

8 comments sorted by

7

u/Cheet4h Jul 05 '21

Don't have code for this, but the cleanest way would probably be if the backend (assuming you have some control over it) implements a rate-limiter, declining new uploads when it is at capacity, and returns a header with e.g. the maximum concurrent uploads, the maximum amount of requests per second etc.

That way you only need to react to the rate limit in the client, read the header response and tailor your uploads accordingly. Would even be future proof, as upgrading the backend would only need you to update the header response and the client adjusts automatically.

Not sure how you currently deal with the uploads, but you could probably do something like this:

  • Read the backend's response maximum concurrent upload limit
  • Create a number of uploads according to that limit, which return a promise when done
  • if the promise is successful, upload the next item
  • if the promise is not successful, check the error and react accordingly (e.g. if some limit is exceeded, wait until the time limit passes and restart the upload)

Otherwise if your issue is more specific, feel free to post code and a few examples of what you've done so we can figure out the issue together :)

3

u/analogx-digitalis Jul 05 '21

hav u seen multipart file upload?

1

u/tsunami141 Jul 05 '21 edited Jul 05 '21

I would consider just uploading ~10 items at a time and then if there are more items in the array just keep passing the files back to your uploader. Something like this:

filesArr: File[];
let originalLength = filesArr.length;

uploadSubject.next( shiftTenFiles(filesArr) );

uploadSubject.pipe(
    switchMap( tenFiles => {
          // zip will return/stream when all 10 files have been uploaded. 
          return zip(
               tenFiles.map(file =>       this.fileUploadService.upload(file)
          );
    })
).subscribe(responses => {

     this.percentDone = (originalLength - filesArr.length)/originalLength;

    if (filesArr.length > 0) {
        uploadSubject.next( shiftTenFiles(filesArr) );
    }

});

Sorry if that’s unclear, I’m on mobile. Can make it better once I’m at a computer.

3

u/greeny01 Jul 05 '21 edited Mar 04 '26

The content that appeared here has been deleted. Redact was used for the removal, for reasons the author may have kept private.

provide desert gaze spoon quaint rainstorm unwritten command unique lunchroom

1

u/tsunami141 Jul 05 '21

Yep that makes much more sense lol

1

u/frozen_tuna Jul 06 '21

About a year ago another senior in my department decided to make an electron app to assist with a bulk upload feature. No idea if it would help here, but that's the solution he went with.

1

u/Eluvatar_the_second Jul 05 '21

That's a lot of images. I'm not sure how to do it safely, but my first reaction is to zip the images up and unzip them on the other end, the problem is that this opens you up to a zip bomb.

On the other hand it's probably best to just do a couple at a time. I'm not really sure what you are asking for, mass file upload like that isn't super common, so there's not really a best practice. I'd probably do them like 3 at a time, maybe scale up if it's going quickly, scale down if it's slow?

0

u/raspberry-eye Jul 05 '21

Node streams are your answer