Bottle Framework Learning (v) file download and upload

Source: Internet
Author: User

Download file

Bottle file Download or use static_file This module, just add a parameter:download=true, or see example!

[Email protected]]# cat download.py#!/usr/bin/envpython#coding=utf-8frombottle Import Route,run,view,static_file @ Route ('/download/<filename:path> ') defdownload (filename): Returnstatic_file (filename,root= '/home/bottle/ Static ', download=true) run (host= ' 0.0.0.0 ', port=8000,debug=true)
[[Email protected]]# ll static/total dosage 100-rw-r--r--. 1 root root 67886 June lufei.jpg-rw-r--r--. 1 root root 30973 June 2 1 suolong.jpg

in the browser, type:http://192.168.116.199:8000/download/suolong.jpg

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C4/wKiom1WGRPuA1C7QAACT8M6rSVg373.jpg "title=" 1.png " alt= "Wkiom1wgrpua1c7qaact8m6rsvg373.jpg"/>

above is the use URL download directly, the following demo uses the link to download:

[Email protected]]# cat Download.py#!/usr/bin/envpython#coding=utf-8frombottle Import route,run,template,static_ File @route ('/download/<filename:path> ') defdownload (filename): Returnstatic_file (filename,root= '/home/ Bottle/static ', download=filename) @route ('/hello ') Defhello (): Return template (' Hello ') run (host= ' 0.0.0.0 ', port= 8000,debug=true)
[Email protected]]# cat views/hello.tpl

in the browser, type:http://192.168.116.199:8000/hello, then click " download file ":

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C0/wKioL1WGRsGC7wE-AADov2oTHmc318.jpg "title=" 2.png " alt= "Wkiol1wgrsgc7we-aadov2othmc318.jpg"/>

Uploading files

when uploading a file in the front-end form form, to add the enctype= "multipart/form-data" attribute,enctype= "Multipart/form-data" is to set the form's MIME encoding. By default, this encoding format is application/x-www-form-urlencodedand cannot be used for file uploads, only multipart/form-data is used. To complete the transfer of file data.

request.files method, get to the table only son file, First assign the object to a variable name, such as uploadfile Save () method to save to the server. Uploadfile.save (save_path,overwrite=true) save_path

[[email protected]]# cat upload.py#!/usr/bin/envpython# Coding=utf-8frombottle import route,run,template,request upload_path= './static ' # Define the save path for the upload file @route ('/upload ') defupload ():     return template (' upload ')  # Use the Get method to return this stencil   @route ('/upload ', method= ' POST ') defdo_upload ():     uploadfile= Request.files.get (' data ')   #获取上传的文件     uploadfile.save (upload_path,overwrite=true) # The overwrite parameter refers to overwriting a file with the same name     return u "upload succeeded, file name:%s, File type:%s"%  (Uploadfile.filename, Uploadfile.content_type)      #filename是获取上传文件文件名, Content_Type is to get the uploaded file type run (host= ' 0.0.0.0 ', Port=8000,debug=true) 
[Email protected]]# cat views/upload.tpl

in the browser, type:http://192.168.116.199:8000/upload

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6E/C4/wKiom1WGRRvThsvQAABb2rk0cDE317.jpg "style=" float: none; "title=" 3.png "alt=" Wkiom1wgrrvthsvqaabb2rk0cde317.jpg "/>

Click to browse to upload file

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6E/C0/wKioL1WGRtCjHMeJAACK_Twkne8237.jpg "style=" float: none; "title=" 4.png "alt=" Wkiol1wgrtcjhmejaack_twkne8237.jpg "/>

This article is from "Gan nan has" blog, please be sure to keep this source http://changfei.blog.51cto.com/4848258/1663965

Bottle Framework Learning (v) file download and upload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.