python - How can I get file name from request.FILE in Django? -
i tried code saving images. saves image @ given path, not file name. how can it?
@csrf_exempt def upload_file(request): if request.method == 'post': form = uploadfileform(request.post, request.files) if 'filename' in request.files: filename = request.files['filename'].name if form.is_valid(): handle_uploaded_file(request.files['file']) return httpresponseredirect('/user_profileform/') else: form = uploadfileform() return render_to_response('user_profile.html', {'form': form }) def handle_uploaded_file(f): destination = open('media/filename', 'wb+') chunk in f.chunks(): destination.write(chunk) destination.close()
it seems want reuse filename save file, can try:
def handle_uploaded_file(f): filename = f.name # name here destination = open('media/'+filename, 'wb+') chunk in f.chunks(): destination.write(chunk) destination.close()
in upload_file
view, there wrong too:
@csrf_exempt def upload_file(request): if request.method == 'post': form = uploadfileform(request.post, request.files) if 'filename' in request.files: # filename = request.files['filename'].name # remove line, don't need if form.is_valid(): handle_uploaded_file(request.files['filename']) # should filename here #.... code ....
hope helps.
Comments
Post a Comment