一样先确认使用者在文件库有哪些文件
确认传送来的文件doc_id是属於使用者
确认文件存在後回传HttpResponse给使用者下载
doc_info/views.py
@login_required
def doc_download(request,doc_id):
user = request.user
Doc_warehouse = doc_warehouse.objects.filter(user_id=user.id)
try:
doc = Doc_warehouse.get(id=doc_id)
upload_file_path = str(doc.upload_file)
print(upload_file_path)
if os.path.exists(upload_file_path):
with open(upload_file_path, 'rb') as file:
response = HttpResponse(file.read(), content_type="application/vnd.ms-excel")
response['Content-Disposition'] = f"inline; filename={os.path.basename(upload_file_path)}"
return response
except Exception as e:
print(e)
return HttpResponseNotFound('You have no access to this file')
网址会去抓downlaod/doc_id参数
doc_info/urls.py
from django.urls import path
from . import views
app_name = "doc_info"
urlpatterns = [
path('doc/download/<int:doc_id>', views.doc_download, name='download'),
]
在使用者个人文件页新增table栏位
超连结将文件的doc_id传到下载功能的网址
templates/doc/user_list.html
{% block content %}
<a href="{% url 'doc_info:main' %}">Main page</a> |
<a href="{% url 'auth_info:profile' %}">My Profile</a> |
<a href="{% url 'account_logout' %}">Logout</a>
<p>Welcome {{ user.first_name }} {{ user.last_name }}</p>
<a href="{% url 'doc_info:create' %}">Create document</a>
<p></p>
<table border = "1">
<thead>
<tr>
<th >Create date: </th>
<th>Last modified date:</th>
<th>Author</th>
<th>Title</th>
<th>Remark</th>
<th>Attachment</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
{% for doc in Doc_warehouse %}
<tr>
<td>{{ doc.create_date }}</td>
<td>{{ doc.last_mod_date }}</td>
<td>{{ doc.user }}</td>
<td>{{ doc.title }}</td>
<td>{{ doc.remark }}</td>
<td><a href="{% url 'doc_info:download' doc_id=doc.id %}">{{ doc.upload_file_name}}</a></td>
<td><a href="{% url 'doc_info:delete' doc_id=doc.id %}" onclick="return confirm('Are you sure you want to delete this?')">Delete</a></td>
{% endfor %}
</tr>
</tbody>
</table>
{% endblock %}
今日目标 限制帧数 Framerate Independent 设计错误 这个是我再次参考Game ...
股市只有两种人:输家跟赢家。 其中长期赢家只占极少部分,若我们不想成为绝大部分的输家,以下几种行为请...
本次要来介绍如何建立Android Studio上的模拟器,以及有哪些优缺点。 首先我认为最大的优点...
SwiftUI 数据流 SwiftUI 作为一个声明式的 UI 框架,帮我们处理了几乎所有关於 介面...
前言 初次参加铁人赛,开赛第一天,先放轻松暖个身,把前辈的文章看过一遍吧! 相关文章 Mike Fa...