0

I'm building a django application that will be operated via desktop application. Key feature for now is sending/storing files. Basically I need a django view with URL on which I can send files with POST and this view will store the files. Currently I have something like this :

def upload(request):
    for key, file in request.FILES.items():
        path = settings.MEDIA_URL + '/upload/' + file.name
        dest = open(path.encode('utf-8'), 'wb+')
        if file.multiple_chunks:
            for c in file.chunks():
                dest.write(c)
        else:
            dest.write(file.read())
        dest.close()
        destination = path + 'files_sent.txt'
        file = open(destination, "a")
        file.write("got files \n")
        file.close

and urlconf:

url(r'^upload/$', upload, ),

that supports sending chunked files. But this doesn't work. Is the code correct ? Should I take a different approach, like providing a model with file field and in this function creating a new model instance instead of writing file to disk ?

mastodon
  • 497
  • 5
  • 11
  • 22
  • 2
    If you are sending a normal POST request, with request.FILES populated, why not just use a Django Forms and `FileField` to let it do all the work? This also makes your app flexible in case you ever want to use the form in another way. – Bartek Oct 20 '10 at 03:10
  • you mean on the receiver side ? post is sent from python app – mastodon Oct 20 '10 at 03:29

1 Answers1

0

Django provides a whole API for receiving and storing files - you should probably read the documentation.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895