我正在使用 PowerShell 通过 API 将文件上传到网站。
在 PS5.1 中,这将使图像采用正确的 B64 编码,由另一端的 API 处理:
$b64 = [convert]::ToBase64String((get-content $image_path -encoding byte))
在 PS7 中,这会因错误而中断:
Get-Content: Cannot process argument transformation on parameter 'Encoding'. 'byte' is not a supported encoding name. For information on defining a custom encoding, see the documentation for the Encoding.RegisterProvider method. (Parameter 'name')
我尝试以其他编码读取内容,然后使用 [system.Text.Encoding]:GetBytes() 进行转换,但字节数组总是不同的。例如
PS 5.1> $bytes = get-content -Path $image -Encoding byte ; Write-Host "bytes:" $bytes.count ; Write-Host "First 11:"; $bytes[0..10]
bytes: 31229
First 11:
137
80
78
71
13
10
26
10
0
0
0
但是在 PowerShell7 上:
PS7> $enc = [system.Text.Encoding]::ASCII
PS7> $bytes = $enc.GetBytes( (get-content -Path $image -Encoding ascii | Out-String)) ; Write-Host "bytes:" $bytes.count ; Write-Host "First 11:"; $bytes[0..10]
bytes: 31416 << larger
First 11:
63 << diff
80 << same
78 <<
71
13
10
26
13 << new
10
0
0
我尝试了其他编码组合,但没有任何改进。谁能建议我哪里出错了?