在我的AngularJS / Python应用程序中,我使用dropzone
上传了一个文件,其物理路径如下:
D:\uploadedFiles\tmpmj02zkex___5526310795850751687_n.jpg'
现在我想为此文件创建一个链接,以便在前端显示和下载。链接看起来像这样:
http://127.0.0.1:8000/uploadedFiles/tmpmj02zkex___5526310795850751687_n.jpg
实现这一目标的最佳方法是什么?
答案 0 :(得分:0)
你可以简单地添加一个url模式。 urls.py
:
from django.conf.urls import url
from . import views
urlpatterns = [
...,
url(r'^uploadedFiles/(?P<file_name>.+)/$', views.download, name='download')
]
例如,控制器中的和download
方法。 here中建议的views.py
:
import os
from django.conf import settings
from django.http import HttpResponse
# path to the upload dir
UPLOAD_DIR = 'D:\uploadedFiles'
...
def download(request, file_name):
file_path = os.path.join(UPLOAD_DIR, file_name)
if os.path.exists(file_path):
with open(file_path, 'rb') as fh:
response = HttpResponse(fh.read(), content_type="application/octet-stream")
response['Content-Disposition'] = 'attachment; filename=' + os.path.basename(file_path)
return response
else:
raise Http404