2

I'm switching from regular files to zip files doing an upload, and was told I'd need to use a header in this format - Content-Type: application/zip.

So, I can get my file to upload properly via curl with the following:

curl --verbose --header "Content-Type: application/zip" --data-binary @C:\Junk\test.zip "http://client.xyz.com/submit?username=test@test.com&password=testpassword&job=test"

However, when I write a simple powershell script to do the same thing, I run into problems - the data isn't loaded. I don't know how to get a good error message returned, so I don't know the details, but bottom line the data isn't getting in.

$FullFileName = "C:\Junk\test.zip"
$wc = new-object System.Net.WebClient -Verbose
$wc.Headers.Add("Content-Type: application/zip")
$URL = "http://client.xyz.com/submit?username=test@test.com&password=testpassword&job=test"
$wc.UploadFile( $URL, $FullFileName ) 
# $wc.UploadData( $URL, $FullFileName ) 

I've tried using UploadData instead of UploadFile, but that doesn't appear to work either.

Thanks, Sylvia

Sylvia
  • 2,578
  • 9
  • 30
  • 37
  • Give this a try: http://stackoverflow.com/questions/8506533/powershell-equivalent-of-curl-http-post-for-file-transfer – Andy Arismendi Feb 07 '12 at 12:30
  • Thanks Andy - I actually did get the simple file upload to work using powershell, but when i switched from csv to zip file it stopped working. Even though when doing the equivalent in curl, I needed the --data-binary switch even with the csv files. – Sylvia Feb 07 '12 at 14:39

2 Answers2

2

I don't necessarily have a solution but I think the issue is that you are trying to upload a binary file using the WebClient object. You most likely will need the UploadData method but I think you are going to have to run the zip file into an array of bytes to upload. That I'm not sure of off the top of my head.

If you haven't, be sure to look at the MSDS docs for this class and methods: http://msdn.microsoft.com/en-us/library/system.net.webclient_methods.aspx

Jeffery Hicks
  • 947
  • 5
  • 8
1

Now that I look at it again I think you need: $wc.Headers.Add("Content-Type", "application/zip") because the collection is key/value paired. Check out this SO question:

WebClient set headers

Also if your still having issues you might need to add a user agent header. I think curl has it's own.

$userAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2;)"
$wc.Headers.Add("user-agent", $userAgent)
Community
  • 1
  • 1
Andy Arismendi
  • 50,577
  • 16
  • 107
  • 124