从Powershell脚本上传多个文件 [英] Upload multiple files from Powershell script

查看:209
本文介绍了从Powershell脚本上传多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < form action =x我有一个可以处理HTML表单的web应用程序, method =postenctype =multipart / form-data> 
< input name =xfatype =file>
< input name =pdftype =file>
< input type =submitvalue =Submit>
< / form>

请注意,有两个 type =file < input> 元素。



如何从Powershell脚本发布脚本?我打算这样做,为服务创建一个简单的测试框架。



我发现 WebClient.UploadFile(),但是只能处理单个文件。



感谢您抽出宝贵时间。

解决方案

。我希望下面的代码对你有帮助。
$ b



祝您好运!请告诉你是否得到它的工作。
$ b

 函数Send-Results {
param (
[Mandatory = $ True,Position = 1]] [ValidateScript({Test-Path -PathType Leaf $ _})] [String] $ ResultFilePath,
[parameter(Mandatory = $ True ,Position = 2)] [System.URI] $ ResultURL

$ fileBin = [IO.File] :: ReadAllBytes($ ResultFilePath)
$ computer = $ env:COMPUTERNAME

#将字节数组转换为字符串(不作任何更改)

$ enc = [System.Text.Encoding] :: GetEncoding(iso-8859-1)
$ fileEnc = $ enc.GetString($ fileBin)

<#
#PowerShell还没有内置的支持'multipart'(即二进制文件上传兼容)
#表单上传。所以我们必须制作一个...

#这是类似于:
#$ curl -i -Ffile=@file.any-Fcomputer = MYPC http:// url

#边界是在发送的数据中保证不存在的任何东西(即字符串足够长)

#注意:协议是非常精确地获得换行数量(CRLF或LF工作)。
#>
$ border = [System.Guid] :: NewGuid()。ToString()#
$ b $ LF =`n
$ bodyLines =(
- $ boundary,
Content-Disposition:form-data; name =`file`$ LF,#filename =是可选的
$ fileEnc,
- $
Content-Disposition:form-data; name =`computer`$ LF,
$ computer,
- $ boundary - $ LF
)-join $ LF

try {
#返回从服务器获得的响应(我们传递它)。

Invoke-RestMethod -Uri $ URL -Method Post -ContentTypemultipart / form-data; boundary =``$ boundary`-TimeoutSec 20 -Body $ bodyLines
}
catch [System.Net.WebException] {
Write-Error(FAILED to reach'$ URL':$ _)
throw $ _
}
}


I have a webapplication that can process POSTing of a html form like this:

<form action="x" method="post" enctype="multipart/form-data">
  <input name="xfa" type="file">
  <input name="pdf" type="file">
  <input type="submit" value="Submit">
</form>

Note that there are two type="file" <input> elements.

How can I script POSTing this from a Powershell script? I plan to do that to create a simple test-framework for the service.

I found WebClient.UploadFile(), but that can only handle a single file.

Thank you for taking your time.

解决方案

I've been crafting multipart HTTP POST with PowerShell today. I hope the code below is helpful to you.

  • PowerShell itself cannot do multipart form uploads.
  • There are not many sample about it either. I built the code based on this and this.
  • Sure, Invoke-RestMethod requires PowerShell 3.0 but the code in the latter of the above links shows how to do HTTP POST with .NET directly, allowing you to have this running in Windows XP as well.

Good luck! Please tell if you got it to work.

function Send-Results {
    param (
        [parameter(Mandatory=$True,Position=1)] [ValidateScript({ Test-Path -PathType Leaf $_ })] [String] $ResultFilePath,
        [parameter(Mandatory=$True,Position=2)] [System.URI] $ResultURL
    )
    $fileBin = [IO.File]::ReadAllBytes($ResultFilePath)
    $computer= $env:COMPUTERNAME

    # Convert byte-array to string (without changing anything)
    #
    $enc = [System.Text.Encoding]::GetEncoding("iso-8859-1")
    $fileEnc = $enc.GetString($fileBin)

    <#
    # PowerShell does not (yet) have built-in support for making 'multipart' (i.e. binary file upload compatible)
    # form uploads. So we have to craft one...
    #
    # This is doing similar to: 
    # $ curl -i -F "file=@file.any" -F "computer=MYPC" http://url
    #
    # Boundary is anything that is guaranteed not to exist in the sent data (i.e. string long enough)
    #    
    # Note: The protocol is very precise about getting the number of line feeds correct (both CRLF or LF work).
    #>
    $boundary = [System.Guid]::NewGuid().ToString()    # 

    $LF = "`n"
    $bodyLines = (
        "--$boundary",
        "Content-Disposition: form-data; name=`"file`"$LF",   # filename= is optional
        $fileEnc,
        "--$boundary",
        "Content-Disposition: form-data; name=`"computer`"$LF",
        $computer,
        "--$boundary--$LF"
        ) -join $LF

    try {
        # Returns the response gotten from the server (we pass it on).
        #
        Invoke-RestMethod -Uri $URL -Method Post -ContentType "multipart/form-data; boundary=`"$boundary`"" -TimeoutSec 20 -Body $bodyLines
    }
    catch [System.Net.WebException] {
        Write-Error( "FAILED to reach '$URL': $_" )
        throw $_
    }
}

这篇关于从Powershell脚本上传多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆