Flask file upload code
import os from flask import Flask, request, redirect, url_for, send_from_directory from import secure_filename UPLOAD_FOLDER = '/tmp/flask-upload-test/' ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif']) app = Flask(__name__) ['UPLOAD_FOLDER'] = UPLOAD_FOLDER def allowed_file(filename): return '.' in filename and \ ('.', 1)[1] in ALLOWED_EXTENSIONS @('/', methods=['GET', 'POST']) def upload_file(): if == 'POST': # check if the post request has the file part if 'file' not in : print 'no file' return redirect() file = ['file'] # if user does not select file, browser also # submit a empty part without filename if == '': print 'no filename' return redirect() if file and allowed_file(): filename = secure_filename() ((['UPLOAD_FOLDER'], filename)) return redirect(url_for('uploaded_file', filename=filename)) return ''' <!doctype html> <title>Upload new File</title> <h1>Upload new File</h1> <form action="" method=post enctype=multipart/form-data> <p><input type=file name=file> <input type=submit value=Upload> </form> ''' @('/uploads/<filename>') def uploaded_file(filename): return send_from_directory(['UPLOAD_FOLDER'], filename) if __name__ == "__main__": (debug=True)
Upload Test
$ curl -F 'file=@"";filename=""' 127.0.0.1:5000
Note: When using the upload file function use POST form-data, the parameter name is the name of the parameter (which is usually agreed upon in advance, not a changed filename), and the value of the parameter is a file (which normally has a filename).
Uploading temporary files
Sometimes the script generates a file to be uploaded, but there is no need to leave it locally, so it uses a temporary file and uploads it directly when it is generated successfully.
Using tempfile
tempfile creates a file in the system's temporary directory and automatically deletes it after use.
import requests import tempfile url = 'http://127.0.0.1:5000' temp = (mode='w+', suffix='.txt') try: ('Hello Temp!') (0) files = {'file': temp} r = (url, files=files, proxies=proxies) finally: # Automatically cleans up the file ()
Using StringIO
Or just use StringIO, which can do the whole thing directly in memory.
import requests from StringIO import StringIO url = 'http://127.0.0.1:5000' temp = StringIO() ('Hello Temp!') (0) = '' # StringIO instances don't have filenames, you need to set them manually, if you don't, the filename will be 'file' when you POST it. files = {'file': temp} r = (url, files=files)
(sth. or sb) else
Adding a piece of code found that allows you to upload multiple attachments with the same name:
files = [ ("attachments", (display_filename_1, open(filename1, 'rb'),'application/octet-stream')), ("attachments", (display_filename_2, open(filename2, 'rb'),'application/octet-stream')) ] r = (url, files=files, data=params)
consultation
10.6. tempfile — Generate temporary files and directories — Python 2.7.12 documentation
Above is the details of Python Flask upload file test example, more information about Python Flask upload file test please pay attention to my other related articles!