4

I'm doing some file validation and want to load an UploadedFile into an external library while it is in the '/tmp' directory before I save it somewhere that it can be executed. Django does the following:

Django will write the uploaded file to a temporary file stored in your system's temporary directory. On a Unix-like platform this means you can expect Django to generate a file called something like /tmp/tmpzfp6I6.upload.

It ihe "tmpzfp616.upload' that I want to be able to get my hands on. UploadedFile.name gives me "" while file.name gives me the proper name of the file "example.mp3".

With the library I am using, I need to pass the filepath of the temporary file to the library, rather than the file itself and so, need the string.

Any ideas?

Thanks in advance.

EDIT: Here's my code:

    from django.core.files.uploadedfile import UploadedFile

    class SongForm(forms.ModelForm): 
        def clean_audio_file(self):
            file = self.cleaned_data.get('audio_file',False)
            if file:
                [...]

                if file._size > 2.5*1024*1024:
                    try:
                        #The following two lines are where I'm having trouble, MP3 takes the path to file as input.
                        path = UploadedFile.temporary_file_path 
                        audio = MP3('%s' %path)
                     except HeaderNotFoundError:
                        raise forms.ValidationError("Cannot read file")

            else:
                raise forms.ValidationError("Couldn't read uploaded file")
            return file 

Using "UploadedFile" I get an AttributeError "type object 'UploadedFile' has no attribute 'temporary_file_path'". If I instead use file.temporary_file_path (just throwing darts in the dark here) I get an IOError:

[Errno 2] No such file or directory: 'bound method TemporaryUploadedFile.temporary_file_path of >'

I realize temporary_file_path is the solution I'm looking for, I just can't figure out how to use it and neither the docs nor google seem to be much help in this particular instance.

Matt Parrilla
  • 3,171
  • 6
  • 35
  • 54

1 Answers1

7

UploadedFile.temporary_file_path

Only files uploaded onto disk will have this method; it returns the full path to the temporary uploaded file.

rzetterberg
  • 10,146
  • 4
  • 44
  • 54
  • I saw that in the docs but I can't get it to work properly... I'll add my code above. Thanks – Matt Parrilla Jul 09 '11 at 13:48
  • 1
    You need to access the temporary_file_path method on a `UploadedFile` instance. If your instance is name file you access the path like this: `file.temporary_file_path()`. – rzetterberg Jul 09 '11 at 18:00
  • 2
    N.B. This seems to work only if your uploaded file is large enough to be *written* to the temporary directory. If it is too small, as far as I understood, the file will be kept in memory and, accordingly, no filepath exists as the file is not on the filesystem. – Shadow Aug 25 '17 at 11:31
  • [Updated link to documentation](https://docs.djangoproject.com/en/2.2/ref/files/uploads/#django.core.files.uploadedfile.TemporaryUploadedFile.temporary_file_path) – Evan Apr 22 '19 at 16:43