I'm trying to POST some data to an Express server using Multer as the file upload middleware.
The request includes a few text fields, an API (.yaml) file and an.xlsx file. I can successfully post the.yaml file and the text fields, but not the excel file.
Below is how I have constructed the multipart/form-data call:
# ----- BUILD & EXECUTE API CALL ------
# Set up the boundary, separator and file encoding to use
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
# Get the filename of the .yaml file
$apiFileName = Split-Path $apiFilePath -leaf
# Encode the .yaml file contents
$apiDefBin = [IO.File]::ReadAllBytes("$apiFilePath")
$apiDefEnc = $enc.GetString($apiDefBin)
# Get the filename of the .xlsx file
$excelFileName = Split-Path $excelFilePath -leaf
# Encode the .xlsx file contents
$excelBin = [IO.File]::ReadAllBytes("$excelFilePath")
$excelEnc = $enc.GetString($excelBin)
# Build the body of the multipart request
$bodyLines = (
"--$boundary",
'Content-Disposition: form-data; name="textField"',
'',
"$textField",
"--$boundary",
"Content-Disposition: form-data; name=`"apiDef`"; filename=`"$apiFileName`"",
'Content-Type: application/octet-stream',
$apiDefEnc,
"--$boundary--",
"Content-Disposition: form-data; name=`"excelFile`"; filename=`"$excelFileName`"",
'Content-Type: application/octet-stream',
$excelFileEnc,
"--$boundary--"
) -join $LF
# Execute the call
Invoke-WebRequest $url `
-Verbose `
-Method Post `
-TimeoutSec 60 `
-ContentType "multipart/form-data; boundary=$boundary" `
-Body $bodylines
The request seems successful:
I am printing req.body
and req.files
on the server, to see what was posted:
{ textField: 'some text here' }
{ apiDef:
[ { fieldname: 'apiDef',
originalname: 'api-def.yaml',
encoding: '7bit',
mimetype: 'application/octet-stream',
destination: 'temp',
filename: 'api-def.yaml',
path: 'temp\\api-def.yaml',
size: 2276 } ] }
So the.yaml file is successfully posted, as is the text field. However, the excel file is not sent.
I have tried changing the content-type, and researched if there is another encoding I should be using, but to no avail.
Is what I'm trying to do possible? What am I doing wrong?
Try taking the two dashes out after the boundary in the line above just before the upload of the xlsx file?
$apiDefEnc, "--$boundary--", <------ THIS BIT "Content-Disposition: form-data; name= "excelFile
"; filename= "$excelFileName
"", 'Content-Type: application/octet-stream',
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.