1

when I'm uploading a file using Powershell Invoke-Webrequest, then the filename gets encoded to base64 when it contains a german umlaut, otherwise it stays in the original encoding. Here's an example:

$path = "C:\test\Peter Müller.txt"
$uploadFormDict = @{}
$uploadFormDict['myfile'] = Get-Item -Path $path
Invoke-WebRequest -Uri "https://www.my-example-url.de/upload" -Method POST -Form $uploadFormDict

The filename that has been uploaded is '=?utf-8?B?UGV0ZXIgTcO8bGxlci50eHQ=?=', so the Base64-encoded string 'UGV0ZXIgTcO8bGxlci50eHQ=?=' of 'Peter Müller.txt' with a prepended '=?utf-8?B?'. If I upload a file named 'Peter Mueller.txt', the filename stays 'Peter Mueller.txt'. How can handle that the filename will not be encoded to Base64?

Thank you!

Tobias
  • 11
  • 2
  • `Get-Item -Path $path` returns a FileInfo **object**, not just a file name. Did you mean to get the content of the file perhaps? – Theo Aug 02 '22 at 11:32
  • I know that Get-Item returns a FileInfo object. This FileInfo object is stored in the dictionary that is passed to the -Form argument of the Invoke-WebRequest (see the Powershell Doc: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-webrequest?view=powershell-7.2#example-6-simplified-multipart-form-data-submission). And the issue I'm referring is that in the request the filename is then Base64-encoded. – Tobias Aug 02 '22 at 11:39
  • Did you save your script in UTF-8 ? – Theo Aug 02 '22 at 11:40
  • [This question](https://stackoverflow.com/questions/21598398/wrong-encoding-on-powershell-invoke-webrequest-post) may be the same issue, but I'm not entirely convinced that it is. – Bacon Bits Aug 02 '22 at 11:59
  • I found a solution. See below. – Tobias Aug 03 '22 at 11:27

1 Answers1

0

I found a solution. For that I had to debug Powershell to see that they use the ContentDispositionHeaderValue and that the Name property of the FileInfo still contains the original name, but the FileName property of the ContentDispositionHeaderValue contains the encoded string (seen here).

In the documentation of ContentDispositionHeaderValue I found that "The FileName property uses MIME encoding for non-ascii characters." Here is a description of MIME encoding, which matches exactly the format of encoded string I have encountered.

The solution was in my case to decode that filename back at the server side.

Tobias
  • 11
  • 2