2014-07-01 61 views
-1

我想一次使用javascript和python上傳多個文件。每當我做ajax請求時,都會發生錯誤(403或禁止)。我不知道到底是什麼問題? 請幫我...使用jquery和python進行異步文件上傳

,這裏是我的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) 
+0

您是否在服務器上啓用了Django日誌記錄功能?通常你可以看到有關403發生的更多細節。或者,如果您在設置中設置了DEBUG = True,則可能會獲得有關該錯誤的更多詳細信息。 – Jacinda

回答

0

我想這是因爲你錯過了CSRF令牌https://docs.djangoproject.com/en/dev/ref/contrib/csrf/

+0

每當我使用CSRF令牌..我得到相同的迴應.. –

+0

確切的錯誤,我越來越..... POST http://127.0.0.1:8000/simple_file_upload/update_file/ 403(FORBIDDEN)jquery- 2.1.1.js:9188 x.support.cors.e.crossDomain.send jquery-2.1.1.js:9188 x.extend.ajax jquery-2.1.1.js:9188 upload 127.0.0.1:8000/simple_file_upload/profile /:80 onclick 127.0.0.1:8000/simple_file_upload/profile/:110 –

+0

它看起來像是csrf標記的錯誤,請閱讀提供的文檔 – fasouto