0

I am trying to upload a 4 GB zip file via below code.

The same code is working for a smaller files, but it is not working with a large file. I think the exception is generated while closing the FileStream.

I am getting this error:

Message:The underlying connection was closed: An unexpected error occurred on a receive. Stacktrace: at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.FtpWebRequest.RequestCallback(Object obj) at System.Net.CommandStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.IO.Stream.Dispose() at System.Net.ConnectionPool.Destroy(PooledStream pooledStream) at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse) at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage) at System.Net.FtpWebRequest.SyncRequestCallback(Object obj) at System.Net.FtpWebRequest.RequestCallback(Object obj) at System.Net.CommandStream.Abort(Exception e) at System.Net.CommandStream.CheckContinuePipeline() at System.Net.FtpWebRequest.DataStreamClosed(CloseExState closeState) at System.Net.FtpDataStream.System.Net.ICloseEx.CloseEx(CloseExState closeState) at System.Net.FtpDataStream.Dispose(Boolean disposing) at System.IO.Stream.Close() at System.IO.Stream.Dispose() at BackupUtility.Program.UploadFileToFTP(String source)

My FTP code is:

private static bool UploadFileToFTP(string source)
{
    try
    {
        String sourcefilepath = source; // e.g. "d:/test.docx"
        String ftpurl = "myftpurl";

        String ftpusername = "ftpusername"; // e.g. username
        String ftppassword = "ftppassword"; // e.g. password

        string ftpfullpath = "mylocalftpFilePath";

        Console.WriteLine("Before Request. ftpfullpath:" + ftpfullpath);
        //  Console.ReadLine();

        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Timeout = 6000000;

        ftp.Method = WebRequestMethods.Ftp.UploadFile;

        FileStream fs = File.OpenRead(source);

        byte[] buffer = new byte[8192];
        using (Stream ftpstream = ftp.GetRequestStream())
        {
            //Stream ftpstream = ftp.GetRequestStream();
            int read = 0;
            while ((read = fs.Read(buffer, 0, buffer.Length)) != 0)
            {
                ftpstream.Write(buffer, 0, read);
            }
            ftpstream.Flush();
        }

        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
    }
    catch (Exception ex)
    {
        return false;
    }
    return true;
}
Manfred Radlwimmer
  • 13,257
  • 13
  • 53
  • 62
milan m
  • 2,164
  • 3
  • 26
  • 40
  • Is there anything in the server's logs? – ProgrammingLlama Nov 08 '17 at 09:15
  • You "think" its crashing on the close - how would you not know? can you not recreate it – BugFinder Nov 08 '17 at 09:17
  • See this : http://cknotes.com/ftp-timeouts-on-large-files/ - it explains why you may get an issue after the transfer is complete. – PaulF Nov 08 '17 at 09:17
  • @BugFinder, the code is terminating from the using (Stream ftpstream = ftp.GetRequestStream()) block and hence the assumption. – milan m Nov 08 '17 at 09:20
  • Also [this](https://answers.microsoft.com/en-us/windows/forum/windows_vista-networking/ftp-problem-can-not-upload-larger-files-no-issue/482e1ecd-fd4e-4c45-95d7-308296ec48ce?auth=1) page recommends setting passive mode – PaulF Nov 08 '17 at 09:23
  • @PaulF 1) Passive/active mode behavior cannot differ for large and small files. 2) The OP is using passive mode already anyway. – Martin Prikryl Nov 08 '17 at 09:31
  • Can you upload large files using a standalone FTP client? + Show us [log file](https://stackoverflow.com/q/9664650/850848). – Martin Prikryl Nov 08 '17 at 09:33
  • @MartinPrikryl: Microsoft suggestion not mine. There is an accepted answer (for a different issue) [here](https://stackoverflow.com/questions/1559232/net-ftpwebrequest-fails-sometimes) which indicates changing passive mode state may have an effect – PaulF Nov 08 '17 at 09:55
  • This [site](http://mattmitchell.com.au/ftpwebrequest-is-broken/) is an interesting read with some alternative library suggestions. – PaulF Nov 08 '17 at 10:00

0 Answers0