2013-08-22 6 views
1

아래 함수를 사용하여 웹 서버에 비디오 파일을 업로드 할 때 다음 예외가 발생합니다. 업로드하는 비디오 파일이 150MB를 초과하여 메모리 사용량이 적어야하므로이 기능을 사용하고 있습니다.VB.NET 업로드 문제 - ContentLength 바이트를 작성해야합니다.

throw되는 오류 : [Begin] GetResponse를 호출하기 전에 ContentLength 바이트를 요청 스트림에 써야합니다.

코드를 몇 번 보았습니다 & 내가 잘못 가고있는 것을 알 수없는 것 같습니다 & 아마 내 실수를 찾기 위해 두 번째 눈이 필요합니다!

기능 :

Friend Function LowMemoryUploader(ByVal FilePath As String) As String 
     ServicePointManager.ServerCertificateValidationCallback = (Function(sender, certificate, chain, sslPolicyErrors) True) 
     Dim oUri As New Uri("http://mysite.com/upload.php") 
     Dim NowTime As String = DateTime.Now.Ticks.ToString().Substring(0, 14) 
     Dim strBoundary As String = "-----------------------------" & NowTime 
     ' Set Filename 
     Dim FileName As String = FilePath.Split(CChar("\")).Last() 
     ' The trailing boundary string 
     Dim boundaryBytes As Byte() = Encoding.ASCII.GetBytes(vbCr & vbLf & "--" & strBoundary & vbCr & vbLf) 
     ' The post message header 
     Dim sb As New StringBuilder() 
     ' Add Variables 
     sb.Append(strBoundary & vbCrLf & "Content-Disposition: form-data; name=""upload""" & vbCrLf & vbCrLf & "1" & vbCrLf) 
     sb.Append(strBoundary & vbCrLf & "Content-Disposition: form-data; name=""upload_file""; filename=""" & FileName & """" & vbCrLf) 
     sb.Append("Content-Type: video/" & FilePath.Split(".").Last()) 
     sb.Append(vbCrLf & vbCrLf) 
     ' Set Header Bytes 
     Dim strPostHeader As String = sb.ToString() 
     Dim postHeaderBytes As Byte() = Encoding.UTF8.GetBytes(strPostHeader) 
     ' The WebRequest 
     Dim oWebrequest As HttpWebRequest = DirectCast(WebRequest.Create(oUri), HttpWebRequest) 
     ' Set Request Settings 
     System.Net.ServicePointManager.Expect100Continue = False 
     oWebrequest.Method = "POST" 
     oWebrequest.UserAgent = Useragent 
     oWebrequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" 
     oWebrequest.ContentType = "multipart/form-data; boundary=---------------------------" & NowTime 
     oWebrequest.AllowAutoRedirect = True 
     oWebrequest.Timeout = 600000 
     oWebrequest.CookieContainer = cookies 
     ' This is important, otherwise the whole file will be read to memory anyway... 
     oWebrequest.AllowWriteStreamBuffering = False 
     ' Get a FileStream and set the final properties of the WebRequest 
     Dim oFileStream As New FileStream(FilePath, FileMode.Open, FileAccess.Read) 
     Dim length As Long = postHeaderBytes.Length + oFileStream.Length + boundaryBytes.Length 
     oWebrequest.ContentLength = length 
     Dim oRequestStream As Stream = oWebrequest.GetRequestStream() 
     ' Write the post header 
     oRequestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length) 
     ' Stream the file contents in small pieces (4096 bytes, max). 
     Dim buffer As Byte() = New Byte(1096) {} 
     Dim bytesRead As Integer = oFileStream.Read(buffer, 0, buffer.Length) 
     While bytesRead <> 0 
      bytesRead = oFileStream.Read(buffer, 0, buffer.Length) 
      oRequestStream.Write(buffer, 0, bytesRead) 
     End While 
     oFileStream.Close() 
     ' Add the trailing boundary 
     oRequestStream.Write(boundaryBytes, 0, boundaryBytes.Length) 
     Dim oWResponse As WebResponse = oWebrequest.GetResponse() 
     Dim s As Stream = oWResponse.GetResponseStream() 
     Dim sr As New StreamReader(s) 
     Dim sReturnString As String = sr.ReadToEnd() 
     ' Clean up 
     oRequestStream.Close() 
     s.Close() 
     sr.Close() 
     Return sReturnString 
    End Function 

답변

0

이 제품은 제대로 보이지 않습니다 : Dim bytesRead As Integer = oFileStream.Read(buffer, 0, buffer.Length) 초기 읽기 않지만, 그것이 바로 루프 내에서 덮어 쓰기 가져옵니다

Dim bytesRead As Integer = oFileStream.Read(buffer, 0, buffer.Length) 
While bytesRead <> 0 
    bytesRead = oFileStream.Read(buffer, 0, buffer.Length) 
    oRequestStream.Write(buffer, 0, bytesRead) 
End While 

참고. 따라서 귀하는 읽은 것보다 적은 블록을 oRequestStream에 쓰고 있습니다.