通过 HTTP 上传大文件 [英] Upload BIG files via HTTP

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

问题描述

我正在尝试使用 PowerShell 将非常大的 VM 映像(5-15 Gb 大小)上传到 HTTP 服务器.

I'm trying to upload really big VM Images (5-15 Gb size) to an HTTP server using PowerShell.

我尝试使用这几种方法(这里链接到 脚本与 net.WebClient.UploadFile带有 Invoke-webRequest 的脚本)

I tried to use for that few methods (here links to script with net.WebClient.UploadFile and script with Invoke-webRequest)

它适用于小于 2GB 的文件,但不适用于大于此值的文件.

It works well for files less than 2GB, but not for files larger than this.

我正在尝试直接使用 httpWebRequest,但我无法将 FileStream 放入其中.

I'm trying to work with httpWebRequest directly but I unable to put FileStream into it.

所以我的问题是:如何将文件流放入 webrequest 中?

或者更一般地说:如何使用 PowerShell 通过 http 上传大文件?

$Timeout=10000000;
$fileName = "0.iso";
$data = "C:\$fileName";
$url = "http://nexus.lab.local:8081/nexus/content/sites/myproj/$fileName";
#$buffer = [System.IO.File]::Open("$data",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read) #Err Cannot convert argument "buffer", with value: "System.IO.FileStream", for "Write" to type "System.Byte[]": 
#$buffer = gc -en byte $data # too much space in memory 
$buffer = [System.IO.File]::ReadAllBytes($data) #Limit 2gb
[System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.Timeout = $timeout
$webRequest.Method = "POST"
$webRequest.ContentType = "application/data"
#$webRequest.ContentLength = $buffer.Length;
$webRequest.Credentials = New-Object System.Net.NetworkCredential("admin", "admin123");

$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($buffer, 0, $buffer.Length)
$requestStream.Flush()
$requestStream.Close()

[System.Net.HttpWebResponse] $webResponse = $webRequest.GetResponse()
$streamReader = New-Object System.IO.StreamReader($webResponse.GetResponseStream())
$result = $streamReader.ReadToEnd()
return $result
$stream.Close() 

推荐答案

默认情况下,HttpWebRequest 在内存中缓冲数据.只需将 HttpWebRequest.AllowWriteStreamBuffering 属性设置为 false,您就可以上传几乎任何大小的文件.在 msdn

By default HttpWebRequest is buffering data in memory. Just set HttpWebRequest.AllowWriteStreamBuffering property to false and you would be able to upload files with almost any size. See more details at msdn

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

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