我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用werkzeug.FileStorage()。
def convert_document(): if (request.files.get('document')): document=Document(request.files.get('document')) elif (not request.files.get('document') and request.form.get('document')): document=Document(FileStorage( StringIO(request.form.get('document')), '{name}.{extension}'.format( name=random_string(), extension=request.form.get('from')) )) mime_type=document.convert(request.form.get('from'), request.form.get('to')) @after_this_request def delete_document_files(response): document.delete_files() return response return send_from_directory( document.converted_file_directory, document.converted_file_name, mimetype=mime_type)
def post(self): file_ = parse_arg_from_requests(arg='image', type=werkzeug.FileStorage, location='files') if not file_: abort(400, "Required form-data param 'image' type=ImageFile") _setup() # Loads model and weights - takes ~2 seconds file_descriptor, filename = tempfile.mkstemp(suffix='.jpg') logging.info('Saving file: %s', filename) file_.save(filename) text_in_image = read_image_and_delete(filename) return jsonify(status='success', text_in_image=text_in_image.split('\n'), version='0.0.1', )
def post(self): parser = reqparse.RequestParser() parser.add_argument("problemID", type=str, required=True, location="json") parser.add_argument("userID", type=str, required=True, location="json") parser.add_argument("file", type=FileStorage, required=True, location="files") entry = parser.parse_args() try: if db.problem.find_one({"_id": ObjectId(entry['problemID'])}) == None: abort(400) if db.user.find_one({"_id": entry['userID']}) == None: abort(400) except: abort(400) problemName = db.problem.find_one({"_id": ObjectId(entry['problemID'])})['name'] gradingFilePath = os.path.join(os.path.join(PROBLEMS_DIR, problemName.lower()), GRADING_SCRIPT) command = "python3 "+gradingFilePath+" \""+entry["file"].stream+"\"" gradingOutput = subprocess.Popen(shlex.split(command.replace('\\','/')), stdout=subprocess.PIPE).communicate()[0] structuredGradingOutput = json.loads(gradingOutput) status_code = None if "score" in structuredGradingOutput: entry["score"] = structuredGradingOutput["score"] entry.pop("file") db.entry.insert_one(entry) status_code = 201 else: status_code = 400 return jsonify(structuredGradingOutput, status=status_code)
def has_file(self): '''Return True iff self.data is a FileStorage with file data''' if not isinstance(self.data, FileStorage): return False # filename == None => the field was present but no file was entered # filename == '<fdopen>' is for a werkzeug hack: # large file uploads will get stored in a temporary file on disk and # show up as an extra FileStorage with name '<fdopen>' return self.data.filename not in [None, '', '<fdopen>']