我想一个上传的文件返回给客户端后,返回文件给客户。Django的REST框架:上传
models.py
file = models.FileField(_('file'), db_index=True, null=True, blank=True, upload_to='files/')
意见
class ContentInfoViewSet(viewsets.ModelViewSet):
queryset = ContentInfo.objects.all()
serializer_class = ContentInfoSerializer
http_method_names = ['get']
@detail_route(methods=['get']) //this is just for testing
def files(self, request, pk=None):
return Response(pk, status=status.HTTP_200_OK)
在这里,我只是用 “文件” 路线尝试。
当我尝试得到“内容信息”。它很好地工作:
[
{
"url": "http://127.0.0.1:8000/users/content-info/1/",
"id": 1,
"date": "2017-01-27T16:21:41.976289Z",
"title": "Hey Hey",
"image_url": "",
"content_url": "",
"file": null
},
{
"url": "http://127.0.0.1:8000/users/content-info/3/",
"id": 3,
"date": "2017-03-21T12:09:32.984119Z",
"title": "NWE",
"image_url": "",
"content_url": "",
"file": "http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf"
}
]
但该URL不起作用。即使我使用授权得到。我不知道我做错了什么。 它没有找到该页面。而且它的逻辑,因为它不是在urls.py(我的意思是http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf)
该解决方案将是巨大的: pdf
当你打开链接,它显示的PDF文件。我认为这会发生,当我按照这个链接“http://127.0.0.1:8000/users/content-info/files/BIAM-51.pdf”
您正在使用Django的REST框架吗? 也许[此帖](http://stackoverflow.com/questions/2294507/how-to-return-static-files-passing-through-a-view-in-django)将有助于 – code22
呀,对不起,我忘了瞄准它。 –
有没有更好的方法来做到这一点? (即使我不使用fileField)我只想在浏览器中显示文件。问题的关键是,我的东东,显示无论是HTML,PDF或MP3 ......,并与内容类型,我得指定类型 –