文件上传

上传文件,在指定路径下,没有此路径创建路径

# 获取文件
upload_file = request.files['file']
 1 def save_file(upload_file, store_name, owner_type):
 2     """
 3     保存文件到服务器
 4     :param upload_file:
 5     :param store_name:
 6     :param owner_type:
 7     :return:
 8     """
 9     try:
10         base_path = "/data10/website/upload"
11         file_path = os.path.join(base_path, owner_type)
12         # 如果文件路径不存在创建路径
13         if not os.path.exists(file_path):
14             os.makedirs(file_path)
15         logging.warning("file_path:{0}".format(file_path))
16         upload_file.save(os.path.join(file_path.encode("utf-8"), store_name.encode("utf-8")))
17         return True
18     except Exception, e:
19         logging.error('save_file  failed'.format(e), exc_info=True)
20         return False
原文地址:https://www.cnblogs.com/shangwei/p/13903043.html