Saghir A. Khatri
Saghir A. Khatri

Reputation: 3128

webclient.UploadData "The underlying Connection was Closed: An unexpected error occured on a recieve"

I am trying to upload image and video using code below, there is no issue with image upload using ftp, but when I am uploading video I get the following error

Error

The underlying connection was closed: An unexpected error occurred on a receive.

Following is the code I am using to upload

Code

 try
            {
                string uploadFileName = Path.GetFileName(FU_Video.FileName);
                uploadFileName = "video." + uploadFileName.Split('.')[1];
                using (WebClient client = new WebClient())
                {
                    string ftpAddres = "ftp://username:pasword@url-path" + fullname;

                    if (!GetAllFilesList(ftpAddres, "username", "password"))
                    {
                        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpAddres);
                        ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
                        ftp.KeepAlive = false;

                        FtpWebResponse response = (FtpWebResponse)ftp.GetResponse();
                    }
                    client.UploadData(new Uri(ftpAddres + "/" + uploadFileName), FU_Video.FileBytes);
                }

and here is the stack trace.

stack Trace

   at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request)
   at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
   at System.Net.WebClient.UploadData(Uri address, Byte[] data)
   at MentorMentee.SignUp.signup.btn_VidPreview_Click(Object sender, EventArgs e) in I:\VS Projects\code\MentorMentee1\MentorMentee\SignUp\signup.aspx.cs:line 469

after searching I read to make connection alive false, but I am getting error on line client.UploadData(uri,byte[]);

please let me know what is wrong with my code? as video is uploaded on ftp, but I get error.

Upvotes: 0

Views: 1709

Answers (1)

Tomas Kirda
Tomas Kirda

Reputation: 8413

I remember having similar issue, but don't remember exactly what made it work. Here is the code that works well for me:

public void Upload(Stream stream, string fileName)
{
    if (stream == null)
    {
        throw new ArgumentNullException("stream");
    }

    try
    {
        FtpWebRequest ftpRequest = CreateFtpRequest(fileName);
        ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;

        using (Stream requestSream = ftpRequest.GetRequestStream())
        {
            Pump(stream, requestSream);
        }

        var ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
        ftpResponse.Close();
    }
    catch (Exception e)
    {
        throw new FtpException(
            string.Format("Failed to upload object. fileName: {0}, stream: {1}", fileName, stream), e);
    }
}

private FtpWebRequest CreateFtpRequest(string fileName)
{
    if (fileName == null)
    {
        throw new ArgumentNullException("fileName");
    }

    string serverUri = string.Format("{0}{1}", ftpRoot, fileName);
    var ftpRequest = (FtpWebRequest)WebRequest.Create(serverUri);
    ftpRequest.Credentials = new NetworkCredential(configuration.UserName, configuration.Password);
    ftpRequest.UsePassive = true;
    ftpRequest.UseBinary = true;
    ftpRequest.KeepAlive = false;

    return ftpRequest;
}

private static void Pump(Stream input, Stream output)
{
    var buffer = new byte[2048];
    while (true)
    {
        int bytesRead = input.Read(buffer, 0, buffer.Length);
        if (bytesRead == 0)
        {
            break;
        }
        output.Write(buffer, 0, bytesRead);
    }
}

Upvotes: 1

Related Questions