Note that there are some explanatory texts on larger screens.

plurals
  1. POFTP download chunk copy
    primarykey
    data
    text
    <p>A while back I asked a question revolving around how to copy a file in chunks from one location to another: <a href="https://stackoverflow.com/questions/10354610/copyfileex-the-parameter-is-invalid-error">CopyFileEx &quot;The parameter is invalid&quot; error</a></p> <p>I received the following code which was quite helpful. </p> <pre><code> static void chunkCopyFile(string source, string destination, int bytesPerChunk) { uint bytesRead = 0; using (FileStream fs = new FileStream(source, FileMode.Open, FileAccess.Read)) { using (BinaryReader br = new BinaryReader(fs)) { using (FileStream fsDest = new FileStream(destination, FileMode.Create)) { BinaryWriter bw = new BinaryWriter(fsDest); byte[] buffer; for (int i = 0; i &lt; fs.Length; i += bytesPerChunk) { buffer = br.ReadBytes(bytesPerChunk); bw.Write(buffer); bytesRead += Convert.ToUInt32(bytesPerChunk); updateProgress(bytesRead); } } } } } </code></pre> <p>However, I now need to convert this code to use FTP instead. I tried the obvious of just passing the FTP path to the filestream but it gave me an error saying "unsupported".</p> <p>I already managed to get the file length, I'm just not sure how I can split the download into chunks. Any help is appreciated as always!</p> <p>Code so far(not much)</p> <pre><code>static void chunkCopyFTPFile(string destination, int bytesPerChunk) { uint bytesRead = 0; fWR = (FtpWebRequest)WebRequest.Create("ftp://" + FTP_SERVER_NAME + "/test.txt"); fWR.Method = WebRequestMethods.Ftp.DownloadFile; fWR.UseBinary = true; fWR.Credentials = new NetworkCredential(FTP_SERVER_USERNAME, FTP_SERVER_PASSWORD); FtpWebResponse response = (FtpWebResponse)fWR.GetResponse(); Stream responseStream = response.GetResponseStream(); StreamReader sR = new StreamReader(responseStream); sR.ReadToEnd(); sR.Close(); response.Close(); } </code></pre> <p>Final code (working):</p> <pre><code>using (Stream responseStream = response.GetResponseStream()) { using (BinaryReader bR = new BinaryReader(responseStream)) { using (FileStream fsDest = new FileStream(destination, FileMode.Create)) { BinaryWriter bw = new BinaryWriter(fsDest); int readCount; byte[] buffer = new byte[bytesPerChunk]; readCount = responseStream.Read(buffer, 0, bytesPerChunk); bytesRead += Convert.ToUInt32(readCount); updateProgress(bytesRead); while (readCount &gt; 0) { bw.Write(buffer, 0, readCount); readCount = responseStream.Read(buffer, 0, bytesPerChunk); bytesRead += Convert.ToUInt32(readCount); updateProgress(bytesRead); } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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