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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:17:17+00:00 2026-05-28T07:17:17+00:00

Sorry, a beginner’s question: I have a very simple function in my Django application

  • 0

Sorry, a beginner’s question: I have a very simple function in my Django application with which I can upload a file from a web browser to my server (works perfectly!). Now, instead of the web browser, I would like to use an iPhone.

I got a bit stuck as I don’t really know how to provide Django with a valid form, i.e. we need a file name and enctype=”multipart/form-data” as far as I understand.

Here is my upload function in Django:

class UploadFileForm(forms.Form):
    file  = forms.FileField()

def handle_uploaded_file(f):
    destination = open('uploads/example.txt', 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            print form
            print request.FILES
            return HttpResponse('Upload Successful')
    else:
        form = UploadFileForm()
    return render_to_response('upload.html', {'form': form})

My template looks like this (upload.html):

<form action="" method="post" enctype="multipart/form-data">
    {{ form.file }}
    {{ form.non_field_errors }}
    <input type="submit" value="Upload" />
</form>

Now, let’s suppose that I want to send a simple txt file from my iPhone app to the sever.
I don’t really know how to:

  1. provide the file name
  2. specify the enctype and
  3. make sure that it’s in a format Django can read

This is how far I got:

NSString *fileContents = [self loadTXTFromDisk:@"example.txt"];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] 
                                initWithURL:[NSURL 
                                             URLWithString:@"http://127.0.0.1:8000/uploadfile/"]];


[request setHTTPMethod:@"POST"];
[request setValue:@"text/xml"   forHTTPHeaderField:@"Content-type"];
[request setValue:[NSString stringWithFormat:@"%d", [fileContents length]] 
                                forHTTPHeaderField:@"Content-length"];
[request setHTTPBody:[fileContents dataUsingEncoding:NSUTF8StringEncoding]];

NSURLConnection *theConnection = [[NSURLConnection alloc] 
                                  initWithRequest:request 
                                  delegate:self];

However, Django will not except this as the form it expects is not valid. Cf. above:

form = UploadFileForm(request.POST, request.FILES)
if form.is_valid(): #this will not be true ...
  • 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-28T07:17:18+00:00Added an answer on May 28, 2026 at 7:17 am

    The enctype from the HTML form should be the Content-Type of the HTTP request. You are currently setting the content type to 'text/xml' instead.

    You will also have to build up the body as a multipart mime object. The answers to this question seem to have some code for that: File Upload to HTTP server in iphone programming

    Your other option, since you have complete control of the client, is to do an HTTP PUT request. That actually looks closer to what you were doing with your original code. Change the method to ‘PUT’, and don’t use a Django form on the other end; just access request.raw_post_data to get the complete file data.

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

Sidebar

Related Questions

Sorry for my beginner's question... What is the easiest way to define procedures, which
Sorry for the beginner question, but I can't figure out cProfile (I'm really new
Sorry I'm beginner, can somebody help me out? <script> var iW = $(document).width(); $(function()
I'm beginner in Java, so, i'm sorry if the question will be too simple
sorry for the beginner's question, but I find it hard to understand: I have
sorry i'm a beginner and i can't determine how good a question this is,
Hi friends i'm beginner and sorry for simple Questions. how can i add new
Sorry for the basic question - I'm a .NET developer and don't have much
Sorry the title isn't more help. I have a database of media-file URLs that
Sorry for posting such a lowly beginner's question, but I just still don't know

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.