Get last modified meta data from uploaded file?

I’m trying to upload a file with request.FILES and then save not just the file but also when the last time this file was modified on the original machine.

This would look something like:

    <form method="post" enctype="multipart/form-data">
        <input type="file" name="myfile">
        <input type="submit" value="Upload now">
    </form>
def upload_file(request):
    if request.method == "POST" and request.FILES["myfile"]:
        myfile = request.FILES["myfile"]
        last_modified = myfile.last_modified()



I know this is currently not a method, but is there a way to get the metadata from the uploaded file? I think it’s possible with raw HTML ( File: lastModified property - Web APIs | MDN ) but I’m looking for something more elegant.

Thanks!

Welcome @christine1902 !

The filesystem information about a file is not sent to the server when a file is uploaded. If you want that data, you will need to retrieve it using JavaScript running in the browser to collect that data and then add it to the submitted form.

That event listener that you’ve linked to is an example of getting the data, you could then have a hidden field in the form to have the JavaScript save it in the form to be submitted.

1 Like

So, you can access the timestamp in your Django view using request. and convert the timestamp into a Python datetime object and save it?!

What do you mean that you are looking for a more “elegant” solution?! A more Pythonic solution or a more Javascript-ish solution?

myfile.last_modified()is not a real method yet, I was just wondering if I was missing something like that method.

Thank you for your quick answer. I think I can work with that.