[英]Powershell: How can I POST excel (.xlsx) files in a multipart/form-data request?
我正在嘗試使用 Multer 作為文件上傳中間件將一些數據發布到 Express 服務器。
該請求包括幾個文本字段、一個 API (.yaml) 文件和一個.xlsx 文件。 我可以成功發布 .yaml 文件和文本字段,但不能成功發布 excel 文件。
下面是我如何構建 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
該請求似乎成功:
我正在服務器上打印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 } ] }
所以.yaml文件成功發布,文本字段也是如此。 但是,沒有發送 excel 文件。
我嘗試更改內容類型,並研究是否應該使用另一種編碼,但無濟於事。
我正在嘗試做的事情可能嗎? 我究竟做錯了什么?
嘗試在上傳 xlsx 文件之前在上一行中的邊界之后取出兩個破折號?
$apiDefEnc, "--$boundary--", <----- THIS BIT "Content-Disposition: form-data; name= "excelFile
"; filename= "$excelFileName
"", 'Content-Type: application /八位流',
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.