开发者

AJAX uploads issue in Django app

I have followed the tutorial posted here in order to get AJAX file uploads on my Django app. The t开发者_开发知识库hing is that it doesn't work, and the closest I could get to the issue is finding out that the save_upload() method raises the following exception: 'WSGIRequest' object has no attribute 'read'. Any ideas on what I am doing wrong?

EDIT: I figured out that this is only works in Django 1.3. Any ideeas on how to make it work in Django 1.2?


I think I have gotten to the bottom of your problem.

1) You are trying to run .read() on a request object, which is not allowed. Instead, you need to run it on request.raw_post_data.

2) Before you can run .read(), which takes a file-like object, you need to convert it from a str to a file-like object.

Try this:

import StringIO
output = StringIO.StringIO()
output.write(request.raw_post_data)

...now you'll be able to run output.read() and get the data you want.

        #loop through, writing more of the file each time
        file_so_far = output.read( 1024 ) #Get ready....
        while file_so_far: #..get set...
          dest.write( file_so_far ) #Go.
          file_so_far = output.read( 1024 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜