0

Multer をファイル アップロード ミドルウェアとして使用して、Express サーバーにデータを POST しようとしています。

リクエストには、いくつかのテキスト フィールド、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ファイルは送信されません。

content-type を変更しようとしましたが、別のエンコーディングを使用する必要があるかどうかを調査しましたが、役に立ちませんでした。

私がやろうとしていることは可能ですか?私は何を間違っていますか?

4

1 に答える 1