简体   繁体   中英

Files getting renamed while using file upload in django

I am using file upload control in my Django application (Code is mentioned below). I know Django uses two FILE_UPLOAD_HANDLERS to process file upload request.

Memory based : If the file size is smaller than 2.5mb than it uses MemoryFileUploadHandler and saves the files in memory.

Disk based : If the file size is larger than 2.5mb then it uses TemporaryFileUploadHandler and saves the files on disk.

I am strictly using 2nd approach by removing MemoryFileUploadHandler from request token. What I have observed is that files are getting renamed before getting saved in the /tmp folder.

sample_01.pdf --> /tmp/tmpqj9jb13w.upload.pdf
sample_02.pdf --> /tmp/tmph1u_6x4r.upload.pdf
sample_03.pdf --> /tmp/tmp8p5uoh4g.upload.pdf
sample_04.pdf --> /tmp/tmpuwis6ar0.upload.pdf
sample_05.pdf --> /tmp/tmpq24bk6tm.upload.pdf

Because of this it is getting very difficult to identify the files. Is there any way to keep the name of the files as it is.

<div class="custom-file">
<input type="file" class="custom-file-input" id="fileupload" name="fileupload" multiple>
<label class="custom-file-label" for="fileupload">Choose files</label>
</div>

if request.method == 'POST':
    files_list = request.FILES.getlist('fileupload')

You can see this documentation about why the name is being generated.

The reason that the TemporaryFileUploadHandler generates just what it says it does: temporary files. Once you get them onto your server it's up to you to decide what you want to do with them (process it, save it, etc..). Essentially you should not be looking in the /tmp folder for anything. Instead you should be using the File Storage API to process the files instead.

It looks like you might try checking the 'name' attribute from the files per this documentation , but here's the complete documentation on handing file uploads .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM