如何使用PowerShell向uploadserver上传文件?
解决PowerShell上传文件到uploadserver的400错误
错误原因是uploadserver要求上传请求采用multipart/form-data格式,且文件对应的表单字段必须命名为files,你之前的命令直接将文件内容作为请求体发送,不符合服务器的接收规则。
正确的上传命令
使用Invoke-WebRequest(或Invoke-RestMethod)的-Form参数构造符合要求的表单请求:
单文件上传
Invoke-WebRequest -Uri http://192.168.0.164:8000/upload -Method Post -Form @{ files = Get-Item "file.txt" }
多文件上传
如果需要同时上传多个文件,可将文件路径放入数组:
Invoke-WebRequest -Uri http://192.168.0.164:8000/upload -Method Post -Form @{ files = @(Get-Item "file1.txt", "file2.jpg", "document.pdf") }
说明
-Form参数会自动处理multipart/form-data的编码,将指定文件以files字段名提交,完全匹配uploadserver的接收要求。
内容的提问来源于stack exchange,提问作者MegaMagnum
相关产品推荐
相关产品推荐

