使用jquery和python上传异步文件

时间:2014-07-01 13:13:42

标签: jquery python django

我想使用javascript和python一次上传多个文件。每当我执行ajax请求时,都会发生错误(403或禁止)。我不知道到底出了什么问题????? 请帮助我...

这是我的index.html ..

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>jQuery File Upload Example</title>
</head>
<body>
<input id="fileupload" type="file" name="files[]" data-url="/media/" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="/static/js/jquery.ui.widget.js"></script>
<script src="/static/js/jquery.iframe-transport.js"></script>
<script src="/static/js/jquery.fileupload.js"></script>
<script>
$(function () {
    $('#fileupload').fileupload({
        dataType: 'json',
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p/>').text(file.name).appendTo(document.body);
            });
        }
    });
});
</script>
<script>
$('#fileupload').fileupload({
    /* ... */
    progressall: function (e, data) {
        var progress = parseInt(data.loaded / data.total * 100, 10);
        $('#progress .bar').css(
            'width',
            progress + '%'
        );
     }
});
</script>
</body> 
</html>

这是我的看法......

def update_file(request):
    print "view is called"
    context = RequestContext(request)
    context_dict = {}
    if request.method== "POST":
        image = request.POST.get('image')
        request.user.userprofile.resume.delete()
        request.user.userprofile.resume = image
        request.user.userprofile.save()
    return render_to_response('index.html', context_dict, context)

1 个答案:

答案 0 :(得分:0)

我认为这是因为您错过了CSRF令牌https://docs.djangoproject.com/en/dev/ref/contrib/csrf/