我正在尝试通过 RightFax Web Api 将 pdf 文件上传到 Right 传真中。我可以通过 PostMan 执行此操作,然后我可以发送附件。当我尝试通过 PowerShell 上传附件时,我只在实际传真前获得对象名称。System.Net.Http.StreamContent。这是我借来的powershell代码:
Add-Type -AssemblyName System.Net.Http
$AuthURL = "http://" + $RESTAPIServer + "/RightFax/API"
$BaseURL = "http://" + $RESTAPIServer + "/RightFax/API/SendJobs"
$AttachURL = "http://" + $RESTAPIServer + "/RightFax/API/Attachments"
$Base = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))
$vCenterSessionURL = $BaseAuthURL
$Header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($RESTAPIUser+":"+$RESTAPIPassword))}
$TARGET_FOLDER_PATH = "\\SomeFolderPath\"
$TokenUri = "http://" + $RESTAPIServer + "/RightFax/API/Login?rememberMe={rememberMe}"
$Token = Invoke-RestMethod -Method GET -Headers $Header -Uri $TokenUri
Get-ChildItem $TARGET_FOLDER_PATH -Filter *.pdf |
Foreach-Object {
$TARGET_FILE_NAME = $_.Name
$LDA_TARGET_FILE_NAME = $TARGET_FOLDER_PATH + $TARGET_FILE_NAME
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization", "Basic $Base")
$fileName = $TARGET_FILE_NAME
$uri = $AttachURL
$filePath = $LDA_TARGET_FILE_NAME
$FileStream = [System.IO.FileStream]::new($filePath, [System.IO.FileMode]::Open)
$fileContent = [System.Net.Http.StreamContent]::new($FileStream)
$boundary = [System.Guid]::NewGuid().ToString()
$LF = "`r`n"
$bodyLines = (
"--$boundary",
"Content-Disposition: attachment; name=`"$fileName`"; filename=`"$filePath`"",
"Content-Type: application/octet-stream",
"Content-Transfer-Encoding: base64$LF",
$fileContent,
"--$boundary--$LF"
) -join $LF
$Attach = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -Body $bodyLines #-InFile $filePath
write-host $Attach
$FileStream.Dispose()
$fileContent.Dispose()
}