Kār*_*nis 6 powershell post file-upload
我们有一个php网站上传图片到服务器,简单的jpg文件与正确的命名.问题是我们需要一次上传几百个,但php一次只接受1个,重命名并上传.我在PS中完成了文件操作,但无法上传.
与上传相关的PHP部分(据我所知)看起来像这样: <form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">
我在这里检查了谷歌相关主题,并得到了这个:
$uri = "http://example.com/"
$pwd = ConvertTo-SecureString 'MyPassword' -AsPlainText -Force
$cred = New-Object Management.Automation.PSCredential ('myuser', $pwd)
$contentType = "multipart/form-data"
$body = @{
"FileName" = Get-Content($uploadPath) -Raw
}
Invoke-WebRequest -Uri $uri -Method Post -ContentType $contentType -Body $body
Run Code Online (Sandbox Code Playgroud)
我检查过$uploadPath
,这是正确的C:\Folder\file.jpg
.我使用我用来登录网站的凭据,我可以通过GUI上传这些图片.
我尝试在POST和PUT之间切换,没有任何变化.
更换http://example.com
用http://example.com/file.jpg
也没有提供任何的区别.不确定,这是使用POST的正确方法.
我们公司有McAffe网关,但我正在用绕过它的用户运行脚本,所以它不会造成这种情况.
我得到的当前错误消息是:"Invoke-WebRequest:底层连接已关闭:接收时发生意外错误."
任何帮助将不胜感激!对不起,如果这已经解决了,我只是错过了一个条目!
PS我也试过这个 - Powershell脚本将日志文件从本地系统上传到http URL,然后返回Exception calling "UploadFile" with "3" argument(s): "An exception occurred during a WebClient request."
sup*_*ime 12
晚了一年,所以你可能不再需要答案,但对我有用的是:
Invoke-RestMethod -Uri $uri -Method Post -InFile $uploadPath -UseDefaultCredentials
Run Code Online (Sandbox Code Playgroud)
我需要使用Windows身份验证作为当前登录的用户... 从这个其他答案看起来,在某些情况下你可以使用-cred,并且必须在其他情况下构建一个Authorization标头.
即使稍后发布,但这些Invoke-RestMethod
解决方案(以及其他一些解决方案)对我不起作用。根据我的尝试和错误,我的服务端点因缺少边界和其他“内容”问题而失败。
我尝试使用 WebClient 的Uploadfile()功能,它对我有用。值得庆幸的是,它很简洁。
$wc = New-Object System.Net.WebClient
$resp = $wc.UploadFile($uri,$uploadPath)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23075 次 |
最近记录: |