Search code examples
djangowebsocketdjango-channelsreal-time-updates

How to Share Files Through Django Channels, now there are incomplete solutions like from S3 buckets and all


I'm developing a chat application in Django and I want to share files through django channels. I just found some solutions that are based on this topic, but all are incomplete. So please help me for figure it out. Now I'm sending messages like the below mentioned code..

document.querySelector('#chat-message-submit').onclick = function(e) {
          let msg = $('#chat-message-input').val()

            var messageInputDom = document.querySelector('#chat-message-input');
            var message = messageInputDom.value;
            chatSocket.send(JSON.stringify({
              'command':'new_message',
              'message': message,
              'from': username,
              
            }));
            

            messageInputDom.value = '';
          }

Solution

  • just convert that file to base64 and send that as json along with it's type, then only you can retrieve it properly from database.

            const reader = new FileReader();
            reader.readAsDataURL(blob)
            reader.onload = (e) => {
              base64_data = e.target.result;
                base64_value(base64_data)
            }
        chatSocket.send(JSON.stringify({
                   'command':'new_message',
                  'message': base64_data,
                  'from': username,
                  'msg_type':type
              
            }));