Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This thread seems to shed <em>some</em> light on large byte streams:</p> <p><a href="http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1af59645-cdef-46a9-9eb1-616661babf90/" rel="nofollow">http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/1af59645-cdef-46a9-9eb1-616661babf90/</a></p> <p>When using 10 threads to send 250mb files at once, you might actually be running out of memory! What spec machine are you using?</p> <h2>UPDATE</h2> <p>I'm all for having a go, so this is what I've done. I created a rapidshare account, but got bored in reading about the complexities of the upload api. Therefore I'm not actually sure if this code works or not. However, it does <em>run</em>. I noticed that running your code above, doesn't seem do <em>do</em> anything during the file read. However, as soon as <code>GetResponseStream</code> is called, it appears to upload the file there and then. Hmm, we want ot to upload it during our stream reading process.</p> <p>So I tried the suggestion on <a href="http://www.wintellect.com/cs/blogs/jeffreyr/default.aspx?p=2" rel="nofollow">Jeffrey Richter's Blog</a> and had a quick look <a href="http://www.codeproject.com/KB/IP/Upload_To_Rapidshare.aspx" rel="nofollow">here</a> as well for some other upload examples. Now I stated above that I didn't dive into the complexities of RapidShares upload api, rather than the streaming operation itself.</p> <p>See the code below. It's identical apart from the introduction of a <code>MemoryStream</code> for the header information and I removed the footer content also (to get the byte count correct).</p> <pre><code> Dim ms As New MemoryStream() Dim formDataTemplate As String = "Content-Disposition: form-data; name=""{0}""{1}{1}{2}" For Each key As String In otherParameters.Keys ms.Write(boundaryBytes, 0, boundaryBytes.Length) Dim formItem As String = String.Format(formDataTemplate, key, newLine, otherParameters(key)) Dim formItemBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(formItem) ms.Write(formItemBytes, 0, formItemBytes.Length) Next key ms.Write(boundaryBytes, 0, boundaryBytes.Length) Dim headerTemplate As String = "Content-Disposition: form-data; name=""{0}""; filename=""{1}""{2}Content-Type: {3}{2}{2}" Dim header As String = String.Format(headerTemplate, fileParameterName, filePath, newLine, contentType) Dim headerBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(header) ms.Write(headerBytes, 0, headerBytes.Length) Dim length As Long = ms.Length length += New FileInfo(filePath).Length request.ContentLength = length Using requestStream As IO.Stream = request.GetRequestStream() Dim bheader() As Byte = ms.ToArray() requestStream.Write(bheader, 0, bheader.Length) Using fileStream As New IO.FileStream(filePath, IO.FileMode.Open, IO.FileAccess.Read) Dim buffer(4096) As Byte Dim bytesRead As Int32 = fileStream.Read(buffer, 0, buffer.Length) Do While (bytesRead &gt; 0) requestStream.Write(buffer, 0, bytesRead) bytesRead = fileStream.Read(buffer, 0, buffer.Length) Loop End Using requestStream.Close() End Using </code></pre> <p>The whole point of this was to pre-allocate the <code>ContentLength</code> property so it starts streaming immediately. Rather than do the work at <code>GetResponseStream</code>, it does the work on the <code>requestStream.Write</code> command. The bytes in the <code>MemoryStream</code> are counted, then written, then the size of the file is added to the <code>length</code> variable and set as the <code>ContentLength</code>. Does this work?!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload