-1

I'm trying to upload a file selected on Input File. However I am not able to get this file in my django view, request.POST and request.FILES somehow are empty, I think the problem is in the jquery code.

This input file has "file_windows" id. Form has "form_upload" id. here is my jquery code:

 $("#file_windows").change(function(){
    var myform = document.getElementById("form_upload");
    var fd = new FormData();
    var file = document.getElementById('file_windows').files[0];
    fd.append('file_win', file);
    token_value=$("input[name=csrfmiddlewaretoken]").val();
    $.ajaxSetup
    ({
        beforeSend: function(xhr, settings)
        {
            xhr.setRequestHeader("X-CSRFToken", token_value);
        }
    });
    $.ajax
    ({
        url:"./ajax",
        method: 'POST',
        type:"POST",
        cache: false,
        enctype: 'multipart/form-data',
        processData: false,
        contentType: false,
        cache: false,
        data:
        {
             "file_win":fd
        },
         success: function(result)
         {
             alert(result);
         }
     });

});

this is my Django view function:

def ajax_file_request(request):
    if request.is_ajax():
        #response=request.FILES["file_win"] multivaluedictkeyerror
        print(request.FILES)
        print(request.POST)
        return HttpResponse("ajax request")
    else:
        respose = "Not Ajax"
        return HttpResponse(respose)

which responses "ajax request" as expected when running my ajax code, but the print output is empty: https://i.stack.imgur.com/E67xZ.png How can i solve this problem?

1 Answers1

0

I fixed it: The problem was in the ajax request.

 data:
    {
         "file_win":fd
    },

it was wrong i have to write "data" in this way

data:fd