文件上传按照日期创建文件夹保存,并对文件重命名,数据库保存

实际项目中的代码实现:

    icon = req.files["icon"]
    if icon:
        curr_time = datetime.datetime.now()
        sub_path = os.path.join(settings.ICONS_FILE_DIRS, curr_time.strftime("%Y-%m-%d"))
        if not os.path.exists(sub_path):
            os.mkdir(sub_path)
        file_all_path = os.path.join(sub_path, icon.filename)
        icon.save(file_all_path)
        content_id_str = str(req_dict['id']).zfill(12)
        req_dict['icon'] = curr_time.strftime("%Y-%m-%d_%H%M%S.%f") + '_' + content_id_str + icon.filename
        os.rename(file_all_path, os.path.join(sub_path, req_dict['icon']))

        req_dict['updated_at'] = curr_time.strftime("%Y-%m-%d %H:%M:%S")

    models.db.content_category.update_one({"id": eval(req_dict.pop("id"))}, {"$set": req_dict})

 

posted @ 2017-12-28 10:08  mlj0503  阅读(833)  评论(0编辑  收藏  举报