Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5972879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:43:14+00:00 2026-05-22T20:43:14+00:00

I’m experimenting with a site that will allow users to upload audio files. I’ve

  • 0

I’m experimenting with a site that will allow users to upload audio files. I’ve read every doc that I can get my hands on but can’t find much about validating files.

Total newb here (never done any file validation of any kind before) and trying to figure this out. Can someone hold my hand and tell me what I need to know?

As always, thank you in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T20:43:15+00:00Added an answer on May 22, 2026 at 8:43 pm

    You want to validate the file before it gets written to disk. When you upload a file, the form gets validated then the uploaded file gets passed to a handler/method that deals with the actual writing to the disk on your server. So in between these two operations, you want to perform some custom validation to make sure it’s a valid audio file

    You could:

    • check if the the file is less then a certain size (good practice)
    • then check if the submitted file has a certain content type (i.e. an audio file)
      • this is pretty useless as someone could easily spoof it
    • then check that the file ends in a certain extension (or extensions)
      • this is also pretty useless
    • try read the file and see if it’s actually audio

    (I haven’t tested this code)

    models.py

    class UserSong(models.Model):
        title = models.CharField(max_length=100)
        audio_file = models.FileField()
    

    forms.py

    class UserSongForm(forms.ModelForm):
         # Add some custom validation to our file field
         def clean_audio_file(self):
             file = self.cleaned_data.get('audio_file',False):
             if file:
                 if file._size > 4*1024*1024:
                       raise ValidationError("Audio file too large ( > 4mb )")
                 if not file.content-type in ["audio/mpeg","audio/..."]:
                       raise ValidationError("Content-Type is not mpeg")
                 if not os.path.splitext(file.name)[1] in [".mp3",".wav" ...]:
                       raise ValidationError("Doesn't have proper extension")
                 # Here we need to now to read the file and see if it's actually 
                 # a valid audio file. I don't know what the best library is to 
                 # to do this
                 if not some_lib.is_audio(file.content):
                       raise ValidationError("Not a valid audio file")
                 return file
             else:
                 raise ValidationError("Couldn't read uploaded file")
    

    views.py
    from utils import handle_uploaded_file

    def upload_file(request):
        if request.method == 'POST':
            form = UserSongForm(request.POST, request.FILES)
            if form.is_valid():
                # If we are here, the above file validation has completed
                # so we can now write the file to disk
                handle_uploaded_file(request.FILES['file'])
                return HttpResponseRedirect('/success/url/')
        else:
            form = UploadFileForm()
        return render_to_response('upload.html', {'form': form})
    

    utils.py

    # from django's docs
    def handle_uploaded_file(f):
        ext = os.path.splitext(f.name)[1]
        destination = open('some/file/name%s'%(ext), 'wb+')
        for chunk in f.chunks():
            destination.write(chunk)
        destination.close()
    

    https://docs.djangoproject.com/en/dev/topics/http/file-uploads/#file-uploads
    https://docs.djangoproject.com/en/dev/ref/forms/fields/#filefield
    https://docs.djangoproject.com/en/dev/ref/files/file/#django.core.files.File

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a jquery bug and I've been looking for hours now, I can't
I've got a string that has curly quotes in it. I'd like to replace
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Does anyone know how can I replace this 2 symbol below from the string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.