通过HTTP上传BIG文件

通过HTTP上传BIG文件

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

问题描述



我试图用很少的方法来使用(5-15 Gb大小)到一个HTTP服务器使用PowerShell。 (此处链接的脚本和


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

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

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

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

So my question is: how to put filestream into webrequest?

Or more generally: how to upload huge file via http with PowerShell?

$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()
解决方案

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上传BIG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:03