首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RightFax使用边界MIME上传pdf文件到Powershell网络服务

RightFax使用边界MIME上传pdf文件到Powershell网络服务
EN

Stack Overflow用户
提问于 2020-04-28 00:35:30
回答 2查看 322关注 0票数 0

我正在尝试上传一个pdf文件到正确的传真通过RightFax网络Api。我可以通过PostMan做到这一点,然后我就可以发送附件了。当我尝试通过PowerShell上传并发送附件时,我只能得到实际传真ex中的对象名称。System.Net.Http.StreamContent。下面是我借用的powershell代码:

代码语言:javascript
复制
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()
}
EN

回答 2

Stack Overflow用户

发布于 2020-05-01 21:14:39

我想通了。我需要对文件进行编码。对于那些在这里实际获得帮助的人,而不是在这里为其他用户的帖子扮演警察,但忽略了添加任何实际的积极有用的评论的人……这是RightFax向传真添加附件的代码。

代码语言:javascript
复制
$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

                 $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
}
票数 0
EN

Stack Overflow用户

发布于 2020-09-21 11:05:01

我正在设置另一个需要base64格式附件的传真服务(SRFax)。

代码语言:javascript
复制
$file = [IO.File]::ReadAllBytes("C:\Users\Path\To\File.pdf") 
$filebase64 = [System.Convert]::ToBase64String($file)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61463759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档