9

We have a php site that uploads pictures to server, simple jpg file with correct naming. Problem is that we need to upload sometimes couple of hundred of them at a time, but php accepts only 1 at a time, renames them and uploads. I have done file operations in PS quite well, but fail to upload.

PHP part related to upload (as far as I can tell) looks like this: <form name='' id='' enctype='multipart/form-data' method='POST' action='/picture_upload.php' target='_self' onsubmit="default_on_submit(event)">

I checked Google, related topics here as well, and got to this:

$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

I have checked $uploadPath and it is correct C:\Folder\file.jpg. I use credentials that I use to log in to site where I can upload these pictures via GUI.

I have tried switching between POST and PUT, with no changes.

Replacing http://example.com with http://example.com/file.jpg also provided no difference. Unsure, which is correct way to use POST.

We have McAffe web gateway in company, but I'm running script with user that bypasses it, so it's not causing this.

Current error message that I'm getting is: "Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a receive."

Any help would be greatly appreciated! And sorry if this has been already solved and I've simply missed an entry!

P.S. I have also tried this - Powershell script to Upload log file from local system to http URL, and it returns Exception calling "UploadFile" with "3" argument(s): "An exception occurred during a WebClient request."

Community
  • 1
  • 1
Kārlis Bergmanis
  • 131
  • 2
  • 3
  • 7

2 Answers2

25

A year late, so you probably don't need the answer anymore, but what worked for me was:

Invoke-RestMethod -Uri $uri -Method Post -InFile $uploadPath -UseDefaultCredentials

Mine needed to use windows auth as the currently logged in user... From this other answer, looks like you can use -cred in some cases and have to build out an Authorization header in others.

superlime
  • 906
  • 8
  • 10
  • Hello ! I'm facing the same issue and I'm trying to work with your solution. I can indeed send some bytes to my server with this command but I'm struggling to transform these bytes into a file. Any advice, suggestion ? Thanks in advance. – May.D Mar 13 '18 at 16:02
  • **EDIT** Got it, just needed to write bytes and save in suitable format. – May.D Mar 13 '18 at 16:16
  • 1
    Hi. Having trouble consuming this at the other end (using nodejs with express in my instance). Any tips on how to get the file out? I am also struggling with what to search for to get help with this. Thanks, J – Jarrod McGuire Feb 15 '19 at 12:26
5

Even later post but these Invoke-RestMethod solutions (and a few others) didn't work for me. My service endpoint was failing with missing boundaries and other "content" issues, depending on my trial and error.

I tried using WebClient's Uploadfile() functionality and it worked for me. Thankfully, it's concise.

$wc = New-Object System.Net.WebClient
$resp = $wc.UploadFile($uri,$uploadPath)
user01CU812
  • 293
  • 3
  • 10
  • 1
    Unfortunately I don't work there anymore and have no access to system, so can't check if this works. Anyway, this looks like nice solution and hopefully will help someone working similar problem in future! – Kārlis Bergmanis Oct 25 '19 at 21:17
  • 1
    @user01CU812- Your answer worked for me. File is uploaded successfully. Suggestion- Just add to show the response in string : [System.Text.Encoding]::ASCII.GetString($resp) – Prodip Sep 09 '22 at 05:20
  • This worked for me since I need something as close to a FormData file post as possible. – thepenguinmaster Jan 23 '23 at 23:52