4 Pluspunkte 0 Minuspunkte
Wie kann ich in Powershell eine Datei an ein HTTP Formular übergeben?
bezieht sich auf eine Antwort auf: Powershell HTTP Request mit Header
von  

3 Antworten

2 Pluspunkte 0 Minuspunkte

Hier ist ein Powershell Script, du musst nur die entsprechenden Variablen und Parameter nach deinen Bedürfnissen anpassen.

$FilePath = "c:\image.jpg"

$boundary = [System.Guid]::NewGuid().ToString()
$TheFile = [System.IO.File]::ReadAllBytes($FilePath)
$TheFileContent = [System.Text.Encoding]::GetEncoding('iso-8859-1').GetString($TheFile)

$LF = "`r`n"

$bodyLines = (
    "--$boundary",
    "Content-Disposition: form-data; name=`"Description`"$LF",
    "This is a text field",
    "--$boundary",
    "Content-Disposition: form-data; name=`"fileToUpload`"; filename=`"image.jpg`"",
    "Content-Type: image/jpeg$LF",
    $TheFileContent,
    "--$boundary--$LF"
) -join $LF

Invoke-RestMethod "http://localhost/index.php" -Method POST -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines
von (716 Punkte)  
0 Pluspunkte 0 Minuspunkte

Verwende das Invoke-RestMethod Cmdlet.

$filePath = "C:\Pfad\Zum\Bild.jpg"
$uploadUrl = "http://example.com/upload.php"

$formData = @{
    fileToUpload = Get-Item $filePath
}

$response = Invoke-RestMethod -Uri $uploadUrl -Method Post -InFile $filePath -Form $formData

Write-Host $response
von  
0 Pluspunkte 0 Minuspunkte

So vielleicht?

$filePath = "C:\Bild.jpg"
$uploadUrl = "http://example.com/upload.php"
$fileContent = Get-Content -Path $filePath -Raw
$fileBase64 = [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($fileContent))
$response = Invoke-RestMethod -Uri $uploadUrl -Method Post -Headers @{ "Content-Type" = "multipart/form-data" } -Body @{
    fileToUpload = $fileBase64
}
Write-Host $response
von