0

The code below works when i upload files around 12kb and below but for some reason anything above that size results in the program freezing and getting an error: "The underlying connection was closed: An unexpected error occurred on a receive"

Is there something I am doing wrong?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Net;

namespace FTP3
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void uploadFile(string filePath)
    {
        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://175.137.158.136" + "/" + Path.GetFileName(filePath));

        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential(username, password);
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FileStream stream = File.OpenRead(filePath);
        byte[] buffer = new byte[stream.Length];

        stream.Read(buffer, 0, buffer.Length);
        stream.Close();

        Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();

        MessageBox.Show("Upload complete!");

    }
    private void button1_Click(object sender, EventArgs e)
    {
        uploadFile(@"C:\Users\User\Desktop\test\data.xlsx");
    }
  }
}
Kevin Lee
  • 27
  • 1
  • 8

1 Answers1

2

you should upload the file in 'chunks', not completely. So please try the following:

using (FileStream fileStream = File.OpenRead(filePath))
        {
            using (Stream reqStream = request.GetRequestStream())
            {
                long curFileStreamPos = 0;
                long chunkSize = 512;

                while (curFileStreamPos < fileStream.Length)
                {
                    if (fileStream.Length - curFileStreamPos < chunkSize)
                        chunkSize = fileStream.Length - curFileStreamPos;

                    byte[] buff = new byte[chunkSize];
                    fileStream.Read(buff, 0, buff.Length);
                    reqStream.Write(buff, 0, buff.Length);

                    curFileStreamPos += chunkSize;
                }

                reqStream.Close();
            }
            fileStream.Close();
        }

I have not tested this snippet, so please verify indexes especially. If you upload a file at once it may happen that the tcp/ip stack does get timeouts due to preparing the data to upload.

Further, this way you do not create big memory objects when you upload files with hundreds of mb (which would end up completely in memory at your solution)

dsdel
  • 1,042
  • 1
  • 8
  • 11
  • tried the code above, same problem occured: small files get uploaded, bigger files cause the connection to close while a 0kb copy is uploaded. Could it be a problem with my machine? @dsdel – Kevin Lee Mar 17 '18 at 05:27
  • Dear Kevin, just wanted to tell you that you should try another remote ftp server and another computer (best would be if the other computer is also using another internet connection). Sometimes firewalls distrub the connection. – dsdel Mar 17 '18 at 08:37
  • Ok will give it a try, thanks @dsdel – Kevin Lee Mar 17 '18 at 14:52
  • good solution. I try and it's ok. Thank @dsdel. P/s: KevinLee, if you use FileZilla for FTP server, you need change FileTransferExpirationTime to higher. – Datusa Feb 28 '22 at 04:46