[英]Powershell: How can I POST excel (.xlsx) files in a multipart/form-data request?
I'm trying to POST some data to an Express server using Multer as the file upload middleware.我正在尝试使用 Multer 作为文件上传中间件将一些数据发布到 Express 服务器。
The request includes a few text fields, an API (.yaml) file and an.xlsx file.该请求包括几个文本字段、一个 API (.yaml) 文件和一个.xlsx 文件。 I can successfully post the.yaml file and the text fields, but not the excel file.
我可以成功发布 .yaml 文件和文本字段,但不能成功发布 excel 文件。
Below is how I have constructed the multipart/form-data call:下面是我如何构建 multipart/form-data 调用:
# ----- 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:我正在服务器上打印
req.body
和req.files
,以查看发布的内容:
{ 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.所以.yaml文件成功发布,文本字段也是如此。 However, the excel file is not sent.
但是,没有发送 excel 文件。
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?尝试在上传 xlsx 文件之前在上一行中的边界之后取出两个破折号?
$apiDefEnc, "--$boundary--", <------ THIS BIT "Content-Disposition: form-data; name= "excelFile
"; filename= "$excelFileName
"", 'Content-Type: application/octet-stream', $apiDefEnc, "--$boundary--", <----- THIS BIT "Content-Disposition: form-data; name=
"excelFile
"; filename= "$excelFileName
"", 'Content-Type: application /八位流',
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.