问题:使用fastapi框架时,如何进行文件上传?

方法:

方法一

import shutil
from fastapi import FastAPI, File, UploadFile  

app = FastAPI()

@app.post("/uploadfile/")
async def create_upload_file(file: UploadFile = File(...)):
    # 这里可以添加保存文件的逻辑
    # 例如,使用 Python 的内置 open() 函数将文件保存到服务器
    # 注意:出于安全考虑,您应该验证文件类型和大小
    with open(file.filename, "wb") as buffer:  
        shutil.copyfileobj(file.file, buffer)  

    return {"filename": file.filename}

阅读全文

问题:按名称排序,如果名称是数字时,10比2排在前面

解决:order by 时name+1

方法:

select * from classes order by name+1 asc, id asc limit 0, 10;

注:增加“id asc”是因为防止order by和limit一起使用时出错排序错乱

阅读全文

问题:因为使用整数作为url查询参数,所以容易被遍历查询

解决:对整数键进行加密

方法:

# 加密
('加密%s' % id).encode('utf-8').hex()

# 简单的加解密
def decryt_hex(h):
    s = bytes.fromhex(h).decode('utf-8')
    return s[2:]

阅读全文

问题:如何生成验证码图片?

方法:

<?php
//必须至于顶部,多服务器端记录验证码信息,便于用户输入后做校验
session_start();
// 图片宽度、调试
$w = 150;
$h = 45;

//默认返回的是黑色的照片
$image = imagecreatetruecolor($w, $h);
//将背景设置为白色的
$bgcolor = imagecolorallocate($image, 255, 255, 255);
//将白色铺满地图
imagefill($image, 0, 0, $bgcolor);

//空字符串,每循环一次,追加到字符串后面  
$captch_code='';

阅读全文