Trigger action after file upload

Hi guys,

I hit one question below, could you please help to give some suggestions? Appreciate.

I want to get duration of a videofile upload by user, then update this “duration” into database after file uploaded.

I find “post_save”, but it looks this execute before the file upload(before this file exist in this server path).

Here is my step:

  1. Create Model include Field: videoFile(FileField) and the duration of this video(FloatField)
  2. After user upload this file into server, I will run my algorithm in the file path and calculate this duration of this file
  3. After get this duration of this file, I need to update this database immediately

So my question is, how to execute my algo immediately after this file already uploaded into server? And then update database.

How are you handling the file upload? Are you doing it through a form where the data gets posted to a FileField within a model? If so, I believe you would have access to that file field within your form - and should be able to process the contents there - maybe run your algorithm in the form’s clean method.
(I’m hoping these are short / small video clips. I’d be concerned about memory usage and timeout issues.)

Thanks KenWhitesell. I’m now using the VUE frontend to POST file to the djangorestframe backend API. If this file not uploaded/exist on the server, I cannot access. So want to access immediately after the file uploaded, since I need to update the duration field immediately.

Hi KenWhitesell, so, any idea can catch the time when the file upload on the server? I find “post_save”, but this run actually before the file exist on the server. So if I use “post_save”, the file actually not stay on the server yet.
Thanks a lot!

You hadn’t mentioned in your original message that you were dealing with DRF. Unfortunately, I’ve never done anything with file uploads in the DRF environment, so I don’t have any specific information to provide.

Superficially it looks like you’re looking to work with either the MultiPartParser or the FileUploadParser - but that’s just what I can quickly find from the documentation. It appears to me that a custom parser would give you the hook you need to process the files.

Ken

Thanks a lot for your suggestions.

Hi KenWhitesell,

I find my question actually is how to access the InMemoryUploadedFile…

The UploadedFiles documentation shows that you can get access to the files through request.FILES in your view.