10

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."

1
  • What is the solution in PS2.0 for this? I see that Invoke-WebRequest is not supported in PS2.0 Commented Aug 28, 2018 at 9:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

4 Comments

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.
EDIT Got it, just needed to write bytes and save in suitable format.
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
Can I put the file in a variable, much like what curl -X POST http://10.0.2.2:8081/upload -F 'files=@/path/to/file' would do?
7

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)

3 Comments

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!
@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)
This worked for me since I need something as close to a FormData file post as possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.