[英]Upload file through url in django
I'm currently creating an app thats supposed to take a input in form of a url (here a PDF-file) and recognize this as a PDF and then upload it to a tmp folder i have on a server. 我当前正在创建一个应用程序,该应用程序应该以url形式输入(此处为PDF文件),并将其识别为PDF,然后将其上传到服务器上的tmp文件夹中。
I have absolutely no idea how to proceed with this. 我绝对不知道该如何进行。 I've already made a form which contains a FileField which works perfectly, but when it comes to urls i have no clue.
我已经制作了一个包含FileField的表单,该表单可以完美地工作,但是当涉及到URL时我却一无所知。
Thank you for all answers, and sorry about the lacking english skills. 感谢您提供所有答案,并对英语能力不足感到抱歉。
The first 4 bytes of a pdf file are %PDF
so you could just download the first 4 bytes from that url and compare them to %PDF
. pdf文件的前4个字节为
%PDF
因此您只需从该url下载前4个字节并将它们与%PDF
进行比较即可。 If it matches, then download the whole file. 如果匹配,则下载整个文件。
Example: 例:
import urllib2
url = 'your_url'
req = urllib2.urlopen(url)
first_four_bytes = req.read(4)
if first_four_bytes == '%PDF':
pdf_content = urllib2.urlopen(url).read()
# save to temp folder
else:
# file is not PDF
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.