2011-02-25 5 views
0

Ajax POST 요청을 만들고 있는데 내보기에서 인식되지 않습니다. views.py에서Django에서 Ajax POST 요청이 실패했습니다.

코드 :

@csrf_exempt 
def upload(request): 
    if request.method == 'POST': 
     form = UploadFileForm(request.POST, request.FILES) 
     if form.is_valid(): 
     #handle_uploaded_file(request.FILES['file']) 
     f = request.FILES['file'] 
      global globalVarForToTrackUpload 
      global globalFileSizeVariable 
     globalFileSizeVariable = f.size 
     filename = "/static/Data/" + f.name 
     destination = open(filename, 'wb+') 
     for chunk in f.chunks(): 
     destination.write(chunk) 
     globalVarForToTrackUpload += len(chunk) 
     destination.close() 
      #return render_to_response('uploadsuccess.html') 
     allValues = str(globalVarForToTrackUpload) + " : " + str(globalFileSizeVariable) 
     return HttpResponse(allValues) 
    else: 
     form = UploadFileForm() 
    return render_to_response('upload.html', {'form': form}) 

내 미들웨어 설정

은 다음과 같습니다

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware', 
    'django.contrib.sessions.middleware.SessionMiddleware', 
    'django.middleware.csrf.CsrfViewMiddleware', 
    'django.contrib.auth.middleware.AuthenticationMiddleware', 
    'django.contrib.messages.middleware.MessageMiddleware', 
    'django.middleware.csrf.CsrfResponseMiddleware', 
) 

내 자바 스크립트 함수는 다음과 같습니다

function submitForm() 

{ 

    //document.forms["myForm"].submit(); 

    xhrPost = getXhrObject(); 
    var arrFiles = document.getElementById('id_file'); 
    var fileToUpload = arrFiles.files[0]; 
    xhrPost.onreadystatechange = function() { 
     if(xhrPost.readyState == 4 && xhrPost.status == 200) 
      document.getElementById("upload-progress-bar").innerHTML = xhrPost.responseText; 
     else 
      document.getElementById("upload-progress-bar").innerHTML = "processing upload..."; 
    } 

    xhrPost.open("POST","/upload.psp/",true); 

    var boundary = "AJAX--------------" + (new Date).getTime(); 
    var contentType = "multipart/form-data; boundary=" + boundary; 
     xhrPost.setRequestHeader("Content-Type", contentType); 
    xhrPost.setRequestHeader("X-CSRFToken", getCookie('csrftoken')); 

    xhrPost.send(fileToUpload); 

    return false; 

} 

사람이 내가 놓친 거지 무엇을 말해 줄 수 있습니까? 내 요청을 "upload"기능에서 "POST"로 다시 계산하지 않는 이유는 무엇입니까?

미리 감사드립니다.

답변

2

보기에서 request.raw_post_data를 사용하십시오. 어떻게 든 이런 식으로 :

if request.is_ajax(): 
    source = request.raw_post_data 
    #Save or/and modify your file 
else: 
    #As usual 

그런데, 나는 청크로 파일을 얻는 방법을 모르겠다. 어쩌면 누군가는 알고 있습니다.

관련 문제