上传下载文件方式

1.form 

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

 

2.ajax发送

django获取文件 request.FILES.get('file', '')

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

获取的格式 list

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

储存文件 存本地 存数据库

with open('filepath', 'wb) 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

 

posted on 2018-07-10 10:56 .Tang 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/tangpg/p/8944710.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值