If you're using Django, just save it into /media/
root, then you can access it via url
First you need to know the config of MEDIA_ROOT
& MEDIA_URL
inside your settings.py.
If it's not exist, then you can create one
[ settings.py ]
MEDIA_ROOT = os.path.join(BASE_DIR, 'myproject/static')
MEDIA_URL = '/media/'
Then, make sure you're already add the media config into your urls.py
from django.conf.urls.static import static
urlpatterns = [
# Project url patterns...
]
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Since you're mentioned this code myfile = request.FILES['file']
, then I assume you're in views.py. Last but not least we need FileSystemStorage API.
There you go
[ views.py ]
from django.core.files.storage import FileSystemStorage
def upload(request):
if request.method == "POST":
csv_file = request.FILE['file']
fs = FileSystemStorage()
name = fs.save(csv_file.name, csv_file)
print(f"file_name = {fs.url(name)}"
return render(request, 'somepage.html')
Access the file via url,
www.yourhost.com/media/file_name