└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ```bash 2 | @echo off 3 | chcp 65001 4 | echo. 5 | setlocal 6 | rem 你需要有github账号和具有gist权限的access_token; 7 | 8 | rem 新建一个txt文件,复制全部代码,将上述的access_token填入代码中,重命名为.bat后缀的文件即可使用; 9 | 10 | rem 按理说不限制上传文件大小,支持txt、yaml等文本文件,支持csv文件; 11 | 12 | rem 要求所有文件的编码为utf8,否则会出现中文和符号乱码; 13 | 14 | rem 实测win10自带的powershell版本5.1上传中文乱码,最好升级你的powershell版本到7+; 15 | 16 | rem 确认你的powershell目录存在的是pwsh.exe还是powershell.exe,若是后者,将代码默认的pwsh替换成powershell。 17 | 18 | rem 替换USER_NAME为你的GitHub用户名,用于拼接你的文件URL 19 | set USER_NAME=你的GitHub用户名 20 | 21 | rem 替换YOUR_ACCESS_TOKEN为你的GitHub访问令牌 22 | set TOKEN=YOUR_ACCESS_TOKEN 23 | 24 | rem 设置上传到指定gist的gistId,不填此项则创建新的gist 25 | set GIST_ID= 26 | 27 | rem 设置上传后的Gist描述 28 | set GIST_DESCRIPTION=你的Gist描述 29 | 30 | rem 检查是否有文件拖入 31 | if "%~1"=="" ( 32 | echo No file provided. 33 | goto :eof 34 | ) 35 | 36 | set "FILE_PATH=%~1" 37 | set "CHUNK_SIZE=1000000" rem 设置每个分片的大小,单位为字节 38 | 39 | pwsh -Command ^ 40 | "$token = '%TOKEN%';" ^ 41 | "$filePath = '%FILE_PATH%';" ^ 42 | "$fileName = [System.IO.Path]::GetFileName($filePath);" ^ 43 | "$fileContent = [System.IO.File]::ReadAllText($filePath);" ^ 44 | "$chunkSize = %CHUNK_SIZE%;" ^ 45 | "$totalLength = $fileContent.Length;" ^ 46 | "$numChunks = [Math]::Ceiling($totalLength / $chunkSize);" ^ 47 | "$gistId = '%GIST_ID%';" ^ 48 | "for ($i = 0; $i -lt $numChunks; $i++) {" ^ 49 | "$start = $i * $chunkSize;" ^ 50 | "$end = [Math]::Min($totalLength, ($i + 1) * $chunkSize);" ^ 51 | "$chunkContent = $fileContent.Substring($start, $end - $start);" ^ 52 | "if ($i -eq 0 -and $gistId.Length -eq 0) {" ^ 53 | "$gist = @{ description = '%GIST_DESCRIPTION%'; files = @{ ($fileName) = @{ content = $chunkContent } }; public = $false };" ^ 54 | "$response = Invoke-RestMethod -Uri 'https://api.github.com/gists' -Headers @{ Authorization = 'token ' + $token } -Method POST -ContentType 'application/json' -Body ($gist | ConvertTo-Json);" ^ 55 | "$gistId = $response.id;" ^ 56 | "} else {" ^ 57 | "$update = @{ description = '%GIST_DESCRIPTION%'; files = @{ ($fileName) = @{ content = $chunkContent } } };" ^ 58 | "$response = Invoke-RestMethod -Uri ('https://api.github.com/gists/' + $gistId) -Headers @{ Authorization = 'token ' + $token } -Method PATCH -ContentType 'application/json' -Body ($update | ConvertTo-Json);" ^ 59 | "}" ^ 60 | "}" ^ 61 | "Write-Output ('文件上传到Gist完成!Gist URL: https://gist.github.com/' + $gistId)"; ^ 62 | "Write-Output ('')"; ^ 63 | "Write-Output ('你的文件URL: https://gist.githubusercontent.com/%USER_NAME%/' + $gistId + '/raw/' + $fileName)"; ^ 64 | "Write-Output ('')"; ^ 65 | "Write-Output ('NOTE: 中文文件名需要URLEncode处理后才能访问,出现红字或者URL不完整可能上传失败了')"; ^ 66 | "Write-Output ('')"; ^ 67 | "Write-Output ('请保留你gistId: ' + $gistId + ' 将之填入本代码以便上传覆盖同名文件!')"; ^ 68 | "Write-Output ('')"; ^ 69 | 70 | endlocal 71 | echo. 72 | echo 上传完成!15秒后自动关闭窗口…… 73 | timeout /t 15 >nul 74 | exit 75 | ``` 76 | --------------------------------------------------------------------------------