0

I am using JSZIP to zip a mesh object coming from THREEJS and then upload it to a django model through an ajax post request. In principle, it is a bit similar than in : Uploading a zip file using POST in Javascript fails silently, but the data I am uploading is a bit more diverse and it currently fails

JS

myButton.click(function(){
    var TempMesh = new THREE.Mesh(effect.generateGeometry(),effect.material); // requires THREE.js
    var zip = new JSZip();
    var the_exporter = new THREE.OBJExporter(); // requires THREE.OBJExporter()
    var result = the_exporter.parse(TempMesh);
    zip.file("Blob.obj", result.obj);
    zip.file("Blob.mtl", result.mtl);
    zip.generateAsync({type:"blob"}).then(
        function (blob) {
            var fileObj = new File([blob],"Blob.zip");
            var csrftoken = document.getElementsByName('csrfmiddlewaretoken')[0].value; // a div in my html template storing a temporary csrf token
            var data = new FormData();
            data.append("csrfmiddlewaretoken", csrftoken);
            data.append("the_file", fileObj);
            $.ajax({
                url: "THE_DJANGO_VIEW_URL",
                method: 'POST',
                data: data,
                contentType: false,
                processData: false,
                enctype: 'multipart/form-data',
                success: function (result, status, xhr) {//...},
                error: function (result, status, xhr) {//...}
            });
        }
    );

});

VIEWS.py

def MyView(request,pk):
    if request.method == 'POST':
        for keyVals in request.POST.keys():
            print('{} : {}'.format(keyVals,request.POST[keyVals]))

once the view receives the Post request, it does not print the key of the file :

csrfmiddlewaretoken : 3QaJCh6VfgB...

Which tells me that the way to send the zip file is not correct. Any idea? This form contains much more data than just the csrf and the zip file and is used to create new instances of a django model.

compmonks
  • 647
  • 10
  • 24

1 Answers1

0

Just realized that sent files are found under request.FILES and not request.POST. Leaving this for anyone running into similar issue.

compmonks
  • 647
  • 10
  • 24