Q:

Django server side code to handle file uploads from ext js 6

I am trying to write code to upload file from Ext JS and my backend is in Django. I need to fetch the file and save on the server. Here is what I tried so far.
     {
        xtype:'form',
        items:[
            {
              id:'uploadfile',
              reference:'uploadfile',
              xtype:'filefield',
              fieldLabel:'Choose file',
              buttonText:'Browse'
            },
            {
              xtype:'button',
              text:'Upload',
              width:'10%',
              margin:'0 500 0 500',
              handler: function() {
              console.log("Upload clicked");
              var form = this.up('form').getForm();
              if(form.isValid()){
                form.submit({
                url: '/file-upload/',
                params: {
                    csrfmiddlewaretoken: Ext.util.Cookies.get('csrftoken');
                },
                waitMsg: 'Uploading your file ...',
                success: function(fp, o) {
                    alert('Success');
                },
                failure: function(form, action) {
                    alert('Failed');
                }
              });
            }
        }
    }

Now this is what Im trying in Django view
@ensure_csrf_cookie
def file_upload(request):
    save_to_file = path.join(settings.DATA_STORE,"uploads","up_file.txt")
    destination     = open(save_to_file , 'wb+')

    ## Here how do I get the uploaded file info from request?
    ## like request.FILES['some_thing']
    ## or request.POST['some_thing']

    for chunk in file.chunks():
        destination.write(chunk)

    destination.close()

How do I get the file to the server? Appreciate help. Thanks

A:

You need to assign a name to your filefield (reference and id are not enough), like name: 'uploadfile'. Then you can access it as request.FILES['uploadfile'] on Django side.

