我很难弄清楚如何通过PowerShell中的HttpListener保存发送给我的视频数据。我有以下内容,我认为只是将其发送回请求者,但我将其保存到MP4文件中时遇到了麻烦。
$req = $request
$body = $req.InputStream
$reader = New-Object System.IO.StreamReader ($body, $req.ContentEncoding)
$msg = $reader.ReadToEnd()
$reader.Close()
[byte[]] $buffer = [System.Text.Encoding]::UTF8.GetBytes($msg)
$res.ContentLength64 = $buffer.Length
$res.StatusCode = 200
$res.OutputStream.Write($buffer, 0, $buffer.Length)
$res.Close()
感谢你的时间eveyrone!
更新
我已经能够用这个来制作文件了,虽然出于某些原因他们在使用8192大小的字节但是PowerShell说它太大了。有了这个,我得到零长度的文件,没有我能说出的错误。
$path = "c:\matthew3.mp4"
$file = New-Object System.IO.FileStream $path,CreateNew
[byte]$bytes = 255
[int]$bytes_read = 0
while ( $bytes_read = $request.InputStream.Read($bytes, 0, $bytes.length) > 0 )
{
$file.Write($bytes, 0, $bytes_read)
}
其实我确实收到了错误:
Exception calling "GetBytes" with "1" argument(s): "Array cannot be null.
Parameter name: chars"
At line:45 char:1
+ $buffer = [System.Text.Encoding]::UTF8.GetBytes($content)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
Exception calling "Write" with "3" argument(s): "Value cannot be null.
Parameter name: buffer"
At line:47 char:1
+ $response.OutputStream.Write($buffer, 0, $buffer.Length)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : ArgumentNullException
答案 0 :(得分:0)
因此,我们公司Nick的主要开发人员在这里指出了正确的方向。
主要的是,对于FileStream对象,我需要添加Write标志,并在InputStream上使用CopyTo方法,然后关闭它们:
$file = New-Object System.IO.FileStream $path,CreateNew,Write
$context.Request.InputStream.CopyTo($file)
$file.Close()
$context.Request.InputStream.Close()