上传下载文件方式

form 

        <form method="post" action="/url" enctype="multipart/form-data"><input type="file" name="file" />
                <input type="submit" value="上传文件" />
        </form>

ajax发送

获取文件 self.requests.get('file', '')

获取的格式 

[{'body': 'aaa', 'content_type': u'text/plain, 'filename':x.py}

储存文件 存本地 存数据库

with open 'filepath' as f:

  f.write(content)

网页通过html标签显示图片视频

 <!-- 图片文件预览 -->
                    {% if files.filename.split('.')[-1] in ['jpg','bmp','png'] %}
                        <p>文件预览:</p>
                        <img style="width: 500px;height: 400px;" src="images/1.jpg">
 
<!-- 音频文件预览 --> {% elif files.filename.split('.')[-1] in ['mp3'] %} <p>文件预览:</p> <audio controls="controls" height="100" width="100"> <source src="/images/1.mp3" type="audio/mp3" /> <source src="/images/2.ogg" type="audio/ogg" /> <embed height="500" width="400" src="song.mp3" /> </audio> <!-- 视频文件预览 --> {% elif files.filename.split('.')[-1] in ['mp4','ogg','webm'] %} <p>文件预览:</p> <video width="500" height="400" controls="controls"> <source src="/images/aa.mp4" type="video/mp4" /> <source src="/images/{{ files.uuid }}" type="video/ogg" /> <source src="/images/{{ files.uuid }}" type="video/webm" /> <object data="/images/{{ files.uuid }}" width="500" height="400"> <embed src="/images/{{ files.uuid }}" width="500" height="400" /> </object> </video>

下载文件到页面 用tornado的StaticFileHandler

handlers = [
    (r'/', MainHandler),
    (r'/images/(.*\.(jpg|mp3|mp4|ogg|png))', StaticFileHandler, {'path': 'files/'}), # 声明路劲
]

 用户下载文件

七牛 https://portal.qiniu.com/bucket/rock1/resource

接口

from qiniu import Auth, put_data


access_key = ''
secret_key = ''
bucket_name = ''


def upload_qiniu_file_content(content):
    q = Auth(access_key, secret_key)
    token = q.upload_token(bucket_name)

    ret, info = put_data(token, None, content)
    return ret['key'], info

猜你喜欢

转载自www.cnblogs.com/tangpg/p/8944710.html