powershell - 如何使用Invoke-RestMethod上传jpg

标签 powershell file-upload

我正在尝试使用 PowerShell commandlet Invoke-RestMethod 将图片发布到 URL。这是我的命令:

$usercreds = Get-Credential
$pic = Get-Content \\server\share\pic.jpg
$uri = http://website/sub/destination

Invoke-RestMethod -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds

我收到一个错误: “该文件不是有效的图像文件。”我也尝试使用 Invoke-WebRequest ,结果相同。 Web 服务器不是我们的,他们这边的技术人员说可以使用curl,但我们不知道如何使用,也没有Linux 机器。我有什么遗漏的吗?我可以毫无问题地打开 jpg,因此它没有损坏或任何其他东西。

我尝试了这个,但是服务器对我大喊:Using PowerShell Invoke-RestMethod to POST large binary multipart/form-data

错误代码:

PS C:\Windows\system32> Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'image/jpg' -Credential $usercreds
Invoke-WebRequest : {"error":{"message":"the file is not a valid image file"},"data":[],"meta":"error"}
At line:1 char:1
+ Invoke-WebRequest -uri $uri -Method Put -Body $pic -ContentType 'imag ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

最佳答案

尝试使用-Infile参数。 Get-Content 将您的文件解释为字符串数组,但只会把事情搞乱。

$usercreds = Get-Credential
$picPath = "\\server\share\pic.jpg"
$uri = http://website/sub/destination

Invoke-WebRequest -uri $uri -Method Put -Infile $picPath -ContentType 'image/jpg' -Credential $usercreds

关于powershell - 如何使用Invoke-RestMethod上传jpg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42395638/

相关文章:

azure - 为什么 Get-AzKeyVaultSecret 返回 "No Such Host Is Known"?

powershell - PowerShell在CMD提示符下执行命令

php - Laravel 在上传时旋转图像

Powershell 启动进程、超时等待、杀死并获取退出代码

function - 如何成功运行 2 个不同的 Powershell 脚本

powershell - 运行旧版本的 Windows PowerShell ISE

java - jsf中图片上传出现NullPointer异常

file-upload - apache commons fileupload 是否支持分块上传?

tomcat - 将文件传输到 Tomcat 位置

javascript - 在 FileUpload 组件中选择文件时在页面上启用按钮