In my django project, I received a media file posted from client. like this
def upload(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
if form.is_valid():
handle_uploaded_file(request.FILES['file'], request.POST['fid'])
return JsonResponse({"result":"ok"})
return JsonResponse({"result":"failed","msg":"unkown"})
def handle_uploaded_file(f, fid):
with open(STEP_DIR + '/' + fid, 'wb+') as destination:
for chunk in f.chunks():
destination.write(chunk)
On the other hand, I want to process this file by another module. And this module will open a file and handle it like this:
Import thirdModule
thirdModule.open('path_to_url').exporter(...)
As the thirdModule will open a file by a given path. So I have to save the file which I had just received from django ?
Is there any way I can process the file directly with out save it.like is:
def handle_uploaded_file(f, fid):
thirdModule.open(convert_media_to_stream(f))
...
if form.is_valid():
data = form.cleaned_data
inmemory_uploaded_file = data['file_field_name']
file_name = str(inmemory_uploaded_file)
process_file_without_saving_into_disk(inmemory_uploaded_file)
# rest of your code
Update
def handle_upload(file, file_name, upload_path):
if not os.path.exists(upload_path):
os.mkdir(upload_path)
file_path = os.path.join(upload_path, file_name)
with open(file_path, 'wb+') as destination:
for chunk in file.chunks():
destination.write(chunk)
return file_path
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.