Upload large file to ftp C#

后端 未结 1 966
野性不改
野性不改 2021-01-25 10:56

I\'m uploading large files to ftp site using this code.

Code

using (FileStream fs = new FileStream(FileLoc, FileMode.Open, FileAccess.Read))
                     


        
相关标签:
1条回答
  • 2021-01-25 11:37

    Here's the code we use for uploading to FTP. It looks very similar to your own. Nevertheleess, I post it for your reference as we haven't had any such reported failures

        private void UploadFile(string fileToUpload)
        {
            Output = new Uri(Path.Combine(Output.ToString(), Path.GetFileName(fileToUpload)));
            FtpWebRequest request = WebRequest.Create(Output) as FtpWebRequest;
            request.Method = WebRequestMethods.Ftp.UploadFile;
            // in order to work with Microsoft Windows Server 2003 + IIS, we can't use passive mode.
            request.UsePassive = false;
            request.Credentials = new NetworkCredential(username, password);
    
            // Copy the contents of the file to the request stream.
            Stream dest = request.GetRequestStream();
            FileStream src = File.OpenRead(fileToUpload);
    
            int bufSize = (int)Math.Min(src.Length, 1024);
            byte[] buffer = new byte[bufSize];
            int bytesRead = 0;
    
            int numBuffersUploaded = 0;            
    
            do
            {
                bytesRead = src.Read(buffer, 0, bufSize);
                dest.Write(buffer, 0, bufSize);
                numBuffersUploaded++;
            }
            while (bytesRead != 0);
    
            dest.Close();
            src.Close();
    
            FtpWebResponse response = (FtpWebResponse)request.GetResponse();
            if (response.StatusCode != FtpStatusCode.ClosingData)
            {
                Console.Error.WriteLine("Request {0}: Error uploading file to FTP server: {1} ({2})", Id, response.StatusDescription, response.StatusCode);
            }
            else
            {
                Console.Out.WriteLine("Request {0}: Successfully transferred file to {1}", Id, Output.ToString());
            }
        }
    
    0 讨论(0)
提交回复
热议问题