Answer the question
In order to leave comments, you need to log in
How to find file path and fix escaping?
def upload_file(request):
if request.method == 'POST':
file = request.FILES['data']
extension = file_type(file)
new_name = 'test_name' + '.' + extension
destination = open('C:\\photo\\' + new_name, 'wb+')
k = str(destination)
file_path = k[26:-2]
for chunk in file.chunks():
destination.write(chunk)
destination.close()
return JsonResponse({
'file_path': file_path,
})
def file_type(file):
return ".".join(file.name.split('.')[-1:])
Answer the question
In order to leave comments, you need to log in
Is there a method to find out the file extension?
import os
destination = open('C:\\photo\\' + new_name, 'wb+')
# не делайте так...
# k = str(destination)
# file_path = k[26:-2]
filename = destination.name # имя файла
filepath = os.path.abspath(filename) # полный путь к файлу
file_extension = os.path.splitext(filename)[1] # вернется кортеж: [0] - имя файла, [1] - расширение
for chunk in file.chunks():
destination.write(chunk)
destination.close()
with open('C:\\photo\\' + new_name, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question