Я пытаюсь ввести файл из поля ввода, временно сохранить его на диск и ответить с повторной загрузкой того же файла.
Для того, чтобы сделать это, я прочитал, что мне нужно ответить браузеру с content-type : application/octet-stream
и content-disposition: attachment; "filename=myfile.extension"
.
Я могу хранить и слушать мои музыкальные произведения c файл в папке / tmp, так что я знаю, что его входная часть работает.
Это мой код в Pyramid:
@view_config(route_name='process')
def process_file(request):
input_file = request.POST['file'].file
input_file.seek(0)
file_path = os.path.join('/tmp', '%s.mp3' % uuid.uuid4())
with open(file_path, 'wb') as output_file:
shutil.copyfileobj(input_file, output_file)
print(f"Wrote: {file_path}")
filename = file_path.split('/')[-1]
print(filename)
f = open(file_path, 'rb')
return Response(body_file=f, charset='UTF-8', content_type='application/octet-stream', content_disposition=f'attachment; "filename={filename}"')
Это мои заголовки ответа: ![Response headers](https://i.stack.imgur.com/BiDW9.png)
И это мое тело ответа: ![Response body](https://i.stack.imgur.com/wYzi2.png)
Однако Chrome / Firefox не запускает загрузку моего двоичного файла. Что я делаю не так?
ОБНОВЛЕНИЕ
Я также безуспешно пытался с FileResponse
из Пирамиды, я все еще не получаю всплывающее окно загрузки.
@view_config(route_name='process')
def process_file(request):
input_file = request.POST['file'].file
input_file.seek(0)
file_path = os.path.join('/tmp', '%s.mp3' % uuid.uuid4())
with open(file_path, 'wb') as output_file:
shutil.copyfileobj(input_file, output_file)
print(f"Wrote: {file_path}")
return FileResponse(file_path, request=request)