Загрузка файлов с помощью React Django? Удается загрузить файл, но содержимое отсутствует, получаем ошибку file correpted
Backend
@api_view(['POST'])
def download_attachment(request):
body_unicode = request.body.decode('utf-8')
body_data = json.loads(body_unicode)
file_name = body_data['file_name']
migration_typeid = body_data['migration_typeid']
attach_type = body_data['AttachmentType']
object_type = body_data['object_type']
featurename = body_data['fname']
fid = body_data['feature_id']
filter_files = Attachments.objects.filter(
Feature_Id=fid, AttachmentType=attach_type, filename=file_name)
filter_values = list(filter_files.values_list())
file_path = filter_values[0]
fl = open(file_path[4], 'rb')
mime_type, _ = mimetypes.guess_type(file_path[4])
response = HttpResponse(fl, content_type=mime_type)
response['Content-Disposition'] = "attachment; filename=%s" % file_name
return response
Frontend(React)
const handleDownload = (att_Type, migtypeid, id, obj_type, att_name, fid) => {
let body = {
file_name: att_name,
migration_typeid: migtypeid,
object_type: obj_type,
AttachmentType: att_Type,
id: id,
fname: detaildata.Feature_Name,
feature_id: fid,
responseType: "blob",
};
let conf = {
headers: {
Authorization: "Bearer " + config.ACCESS_TOKEN(),
"Content-Type": "application/json",
},
};
// console.log(conf.headers);
axios
.post(`${config.API_BASE_URL()}/api/download_att`, body, conf)
.then((res) => {
fileDownload(res.data, att_name);
})
.catch((err) => { });
};
я могу загрузить файл, но содержимое не приходит в файл для файлов txt он работает, но другие файлы, кроме txt не поддерживаются, получаю ошибку corrupted
может ли кто-нибудь сказать мне решение, как это работает?