简体   繁体   English

Powershell:如何在多部分/表单数据请求中发布 excel (.xlsx) 文件?

[英]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.bodyreq.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.

相关问题 VBA发布请求错误-多部分/表单数据 - VBA post request error - multipart/form-data 如何使用python中的POST请求在multipart/form-data中发送excel文件? - How to send excel file in multipart/form-data with POST Requests in python? 将多部分/表单数据发布到端点 - Post multipart/form-data to endpoint 如何使用 WinHTTPRequest 在 Excel 中使用 VBA 发送表单数据 POST 请求 - How to send a form-data POST request with VBA in Excel using WinHTTPRequest 从 ASP.NET Core HttpContext.Request 的 multipart/form-data 内容读取 excel 文件? - Reading excel file from ASP.NET Core HttpContext.Request of multipart/form-data content? 如何使用Power Query的Web发布目录/表单数据 - How to POST a multipart/form-data using Power Query's Web.Contents 如何从 Fastify POST 请求正文中检索 excel.xlsx 数据? - How to retrieve excel .xlsx data from Fastify POST request body? VBA POST请求发送包含上载文件的多部分表单数据 - VBA POST request sending multipart form data with uploading file 如何在没有Excel或第3方工具的情况下将xlsx文件读入.net? - How can I read xlsx files into .net without Excel or 3rd party tools? 如何使用 pandas 编写 python 脚本来迭代具有多张工作表的 Excel.xlsx 文件? - How can I write a python scripts using pandas to iterate over Excel .xlsx files with multiple sheets?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM