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 7942995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:08:32+00:00 2026-06-04T00:08:32+00:00

I am uploading a file via ajax, and then creating a SimpleUploadFile object and

  • 0

I am uploading a file via ajax, and then creating a SimpleUploadFile object and passing it to replace request.FILES — this does not pass form.is_valid(). I’ve logged the SimpleUploadedFile dictionary and request.FILES replacement below; as you can see, the SimpleUploadedFile object is able to be created:

SimpleUploadedFile [This does not work.]:
{'book_pics': <SimpleUploadedFile: movies.jpg (text/plain)>} # Yielded by printing uploaded_file

When I don’t use ajax and simply use the form to submit the file [I did this out of curiousity], here is what uploaded_file looks like:

request.FILE generated InMemoryUploadedFile [This works.]:
<MultiValueDict: {u'book_pics': [<InMemoryUploadedFile: movies.jpg (image/jpeg)>]}>

Any idea of how I could get the SimpleUploadedFile to validate?

def add_book(request):
    if request.method == 'POST':
        # Grabbing the file uploaded via ajax earlier.
        fh = File(file(settings.MEDIA_ROOT + request.POST['book_pics']))
        uploaded_file = {'book_pics':SimpleUploadedFile(fh.name, fh.read())}
        form = BookForm(data=request.POST, files=uploaded_file)
        if form.is_valid():
            print >> sys.stderr, "Form is valid."
            form.save()
            return render(request, 'generic_message.html', {'message': 'Succesfully added book.'})
        else:
           ...
    else:
        ...

According to these docs, SimpleUploadedFile should work: https://docs.djangoproject.com/en/dev/ref/forms/api/

EDIT:

When I print f.clean(my_file) I get the following error, context below. This happens with and without setting filetype in my_file:

fh = File(file(settings.MEDIA_ROOT + request.POST['book_pics']))
my_file = UploadedFile(fh.name, fh.read(), 'image/jpeg')
f = forms.FileField()
print >> sys.stderr, f.clean(my_file)
# f.clean(my_file) Returns an error: 

DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x92 in position 18: invalid 
start byte. You passed in '<UploadedFile: 
X\x06\x18\x92HI\xde\xf0\x00\xdd\x7f\n\x8e\xf9\x8f\xe5\x00\x92IGa\xaa\xce\xfe_\xa4\x04\xe
3\xca\x1a,\x97\x99\xd0\xda\x02\xe3e\xa8\xbb=\x99\xe0\x00\x0b\xfd?
Q\x00\x02\x80\x00*\xe6\xf7\xb0\xd6\xfb@\x06\xd2\x1f7\xfd\xaf\x00\x04Iw}\x8b\x08\xbcxB\xc
bj\x1e\xa3\x7f1\xf9\xc3\xc3\xef"r\xfb\xcc"T\xa2\x01r\xe7X\xb0\xa3\xc4r\xea\xdf\x9c\x00>\
x96K\x0b\xee\x07\xd26<\xa0\r\xb8\xf2?\xa4\\\x94\xf96\xe43\x1f\xc5\xfa\x1f\xd8@ 
t\xe9\xea\x7f8\x00p?S\xf9\xc0\x01\xc6\xaa\x1b\x06a\xca-
\x1f\xba\x88\xa6\xedn,|\'\xa2\xd8t\xb0\x862\\\xb0\xfa\x17\x16<\xf7\x80\xc1\xce\xc3\xcc\x
fe\x91Xp\x02\xc5\x92\x96\xb3\xa9\x8bo\xac8\x0eQ\xa1\xf3\x80\x07(\xf8GR\xf1x\xf0\x80($\xa
8\x02K76\xda4\x03h\x07\x9f\xed\x00\x07\x1f\x12F\xc7\xfbC\xc3\x90\x16\t\xca\xeeu;\xf4\x8a
\x92\x9f#\xa4\x12Ar\xf7\x80A\xcaId\xdfR\xc7\xae\xb0\xf0\x01i%\xc5\xf7\x8a\x80PI\t\xb9\xb
9 \xfd`\x01\xc2I\xe6}\x81\x1b\x7f*7\x8a\x1c\'O|\x84\\\xc1\xc...
  • 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-06-04T00:08:33+00:00Added an answer on June 4, 2026 at 12:08 am

    From one of my tests:

    name = 'filename'
    f = file(self.TEST_ROOT + name)
    file_data = {'file':SimpleUploadedFile(f.name,f.read())}
    data = {}
    form = self.TestForm(data,file_data)
    self.assertTrue(form.is_valid())
    

    This test is passing. My code is almost identical to yours, except i’m not passing a File but a file to the form. And i do not name the arguments passed. Both differences don’t make a difference, i have tried that. Your problem is that in

    file(settings.MEDIA_ROOT + request.POST['book_pics'])
    

    request.POST[‘book_pics’] does contain the filename, but your file is not in MEDIA_ROOT. It is in the location where django puts temporary uploaded files, or, if it is a small file, it is in memory. Either way the path you create with your code does not point to the file.

    If you try to send the file via ajax, and not with a form, you can do that with a solution like this one: http://kuhlit.blogspot.de/2011/04/ajax-file-uploads-and-csrf-in-django-13.html Have a look at the django code somewhere in the middle of that page.

    Quite complex, but it works.

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

Sidebar

Related Questions

In my project I have a file uploading feature. Files are uploaded via FTP.
i'm following this tutorial: http://net.tutsplus.com/tutorials/javascript-ajax/uploading-files-with-ajax/comment-page-1/#comments to learn how to upload multiple files via ajax.
I want to upload a text file received via AJAX request in Symfony2 (using
I am uploading a file via php move_uploaded_file function and then the file is
I'm uploading an image file via AJAX ajaxFileUpload plugin which uses iframe to submit
When uploading a file (jpeg) via a form in IE7 I am seeing a
So i'm uploading any image file via my great upload form and it uploads
I have found numerous examples on uploading a file to a web server via
I am uploading file(pdf for now) like this: (It is uploading content of file
I am uploading audio file from server, I want the name of files should

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.