我有一个用于测试API的脚本,它返回base64编码的图像.我目前的解决方案是这样.
$e = (curl.exe -H "Content-Type: multipart/form-data" -F "image=@clear.png" localhost:5000)
$decoded = [System.Convert]::FromBase64CharArray($e, 0, $e.Length)
[io.file]::WriteAllBytes('out.png', $decoded) # <--- line in question
Get-Content('out.png') | Format-Hex
Run Code Online (Sandbox Code Playgroud)
这有效,但我希望能够在PowerShell中本地编写字节数组,而不必从[io.file]中获取.
我在PowerShell中编写$ decode的尝试都导致编写了一个整数字节值的字符串编码列表.(例如)
42
125
230
12
34
...
Run Code Online (Sandbox Code Playgroud)
你怎么做得好?
Tan*_*ett 30
Set-Content cmdlet(现在?)允许您使用Byte编码将原始字节写入文件:
$decoded = [System.Convert]::FromBase64CharArray($e, 0, $e.Length)
$decoded | Set-Content out.png -Encoding Byte
Run Code Online (Sandbox Code Playgroud)
Mar*_*son 17
Powershell Core(v6 及更高版本)不再有-Encoding byte选项,因此您需要使用-AsByteStream,例如:
Set-Content -Path C:\temp\test.jpg -AsByteStream
Run Code Online (Sandbox Code Playgroud)
Ves*_*per 11
运行C#程序集是PowerShell的原生程序,因此您已经在"本机"中将字节写入文件.
如果你坚持,你可以使用类似的结构set-content test.jpg -value (([char[]]$decoded) -join ""),这有一个缺点,即在写入数据的末尾添加#13#10.使用JPEG,它是可以忍受的,但其他文件可能会因此更改而损坏.因此,请坚持使用.NET的字节优化例程,而不是搜索"本机"方法 - 这些已经是原生的.
| 归档时间: |
|
| 查看次数: |
18027 次 |
| 最近记录: |