我想允许用户从应用程序上传文件,它必须通过 WebSocket 连接,我很难找到解决方案,到目前为止我找到的所有解决方案都需要设置 Content-Type 标头,而对于 WS 连接我不能无论如何都要找到这样做
上传文件的 JavaScript 代码:
const fd = new FormData();
fd.append('uri', uri)
// I get the URI from user gallery expo-image-picker
// The URI looks like this: file:/data/user/0/host.exp.exponent/cache/ExperienceData/%2540zeyadshaban%252Forgachat/ImagePicker/394f4262-184d-4adc-9a51-db32afcb86a2.png
JSON.stringify({
fd,
type: 'image',
})
在后端(consumers.py)中,我尝试直接保存文件,但它一直抛出错误
elif json_data.get('type') == 'image':
my_model_instance.image = json_data.get('fd')
await sync_to_async(my_model_instance.save)()
当我在终端中检查 json_data.get('fd') 时,我得到了这个
>> json_data.get('fd')
{'_parts': [[...]]}