Powershell使用边界MIME将pdf文件上传到RightFax Web服务

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Powershell使用边界MIME将pdf文件上传到RightFax Web服务相关的知识,希望对你有一定的参考价值。

我正在尝试通过RightFax Web Api将pdf文件上传到Right传真中。我可以通过PostMan进行此操作,之后可以发送附件。当我尝试通过PowerShell上传时,我发送附件时,仅在实际的传真ex中获得了对象名称。 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()

答案

我知道了。我需要对文件进行编码。对于那些实际上是在这里寻求帮助的人,而不是在这里是为其他用户的帖子打招呼,却忽略了添加任何实际的积极有用的评论……的人。这是RightFax在传真中添加附件的代码。

$ AttachURL =“ http://” + $ RESTAPIServer +“ / RightFax / API /附件”$ Base = [System.Convert] :: ToBase64String([System.Text.Encoding] :: UTF8.GetBytes($ RESTAPIUser +“:” + $ RESTAPIPassword))$ vCenterSessionURL = $ BaseAuthURL

$ Header = @ “ Authorization” =“基本” + [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-方法GET -Headers $ Header -Uri $ TokenUri

Get-ChildItem $ TARGET_FOLDER_PATH-过滤器* .pdf |Foreach对象

              $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

             $fileRead = [IO.File]::ReadAllBytes($filePath)
             $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
             $fileEnc = $enc.GetString($fileRead)
              $boundary = [System.Guid]::NewGuid().ToString()

             $LF = "`r`n"
             $bodyLines = (
              "--$boundary",
              "Content-Disposition: form-data; name=`"$fileName`"; filename=`"$ResultFilePath`"",   # filename= is optional
              "Content-Type: application/pdf$LF",
              $fileEnc,

             "--$boundary--$LF"
             ) -join $

   $Attach = Invoke-RestMethod -Uri $uri -Headers $headers -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -TimeoutSec 20 -Body $bodyLines
  Write-host $Attach

以上是关于Powershell使用边界MIME将pdf文件上传到RightFax Web服务的主要内容,如果未能解决你的问题,请参考以下文章

Powershell 检查 MIME 是不是存在于 IIS 中

将 Excel 工作表导出到 PDF 文件时出现奇怪的 Powershell 行为

服务器上的 PHP 文件上传将 mime-type 更改为 application/octet-stream

jsp打开PDF

Symfony:如何在 ppt 和 pptx 上获得正确的 mime 类型

如何使用 Powershell Compress-Archive 仅压缩文件夹中的 pdf?