Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491263
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:57:04+00:00 2026-05-29T15:57:04+00:00

I’m prototyping an ftp utility that will save a user’s progress, and re-upload a

  • 0

I’m prototyping an ftp utility that will save a user’s progress, and re-upload a given file from where they started if their internet were to disconnect. (It’s for clients with slower connections).

The idea is to open a file stream and ftp stream, and write to the ftp stream in chunks of memory. If an exception rises (i.e. an IOException from a disconnect), then the number of bytes written to the ftp server will be saved in a log file and read on start up.

This code works great if the transaction is cancelled, but if the client is to disconnect, then the ftp stream is never cleaned up on the server end – so I receive…

The remote server returned an error: (550) File unavailable (e.g., file not found, no access).

When re-requesting the ftp stream for the given file. The code looks like

     //Create FTP Web Request
     reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(builder.ToString()));
     reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
     reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
     reqFTP.UsePassive = true;
     reqFTP.UseBinary = false;
     reqFTP.KeepAlive = false;
     reqFTP.ContentLength = fileInf.Length;
     reqFTP.ReadWriteTimeout = 5000;
     reqFTP.Timeout = 5000;

     using (ProgressDialog progressDialog = new ProgressDialog())
     {
        progressDialog.backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        progressDialog.backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        progressDialog.backgroundWorker1.FileName = filename;
        progressDialog.ShowDialog();
     }
  }

  private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
  {
     FTPBackgroundWorker worker = sender as FTPBackgroundWorker;

     //Get file progress (if user canceled or crashed)
     worker.NumBytesRead = GetFileProgress(worker.FileName);
     reqFTP.ContentOffset = worker.NumBytesRead;

     const int buffLength = 2048;
     byte[] buff = new byte[buffLength];
     int contentLen;

     using (worker.FileStream = fileInf.OpenRead())
     {
        worker.FileStream.Position = worker.NumBytesRead;
        worker.FTPStream = reqFTP.GetRequestStream(); //Exception occurs

        while (true)
        {
           bool throwException = false;
           if (worker.CancellationPending)
           {
              e.Cancel = true;
              break;
           }

           contentLen = worker.FileStream.Read(buff, 0, buffLength);
           if (contentLen == 0)
              break;

           //write file to ftp stream
           worker.FTPStream.Write(buff, 0, contentLen);
           worker.NumBytesRead += contentLen;

           //For testing purposes
           if (throwException)
              throw new Exception("user disconnected!");
           worker.ReportProgress((int)(((double)worker.NumBytesRead / fileInf.Length) * 100));
        }
        worker.FileStream.Close();
        worker.FTPStream.Close();
     }
  }

  void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
  {
     FTPBackgroundWorker worker = sender as FTPBackgroundWorker;
     if (e.Error != null)
     {
        MessageBox.Show(String.Format("Error occured uploading {0}: {1}",worker.FileName, e.Error), "Error");
        if (worker.FileStream != null)
           worker.FileStream.Close();
        if (worker.FTPStream != null)
           worker.FTPStream.Close();

        if(worker.NumBytesRead > 0)
        {
           MessageBox.Show("Progress has been saved", "Notification");
           WriteToLogFile(worker.FileName, worker.NumBytesRead);
        }
     }
     else if (e.Cancelled)
     {
        if (worker.FileStream != null)
           worker.FileStream.Close();
        if (worker.FTPStream != null)
           worker.FTPStream.Close();

        MessageBox.Show("Upload Canceled", "Cancel");
        if (worker.NumBytesRead > 0 && MessageBox.Show("Would you like to save your upload progress?", "Notification", MessageBoxButtons.YesNo) == DialogResult.Yes)
           WriteToLogFile(worker.FileName, worker.NumBytesRead);
     }
     else
     {
        RemoveFromLogFile(worker.FileName);
        MessageBox.Show("Upload Complete", "Success");
     }
  }

My question is: Is there a way to check if there’s a handle on the server end that’s not letting go of a file path and remove it? Or am I approaching the problem with the wrong methodology?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:57:05+00:00Added an answer on May 29, 2026 at 3:57 pm

    Why not decrement the timeout to a value that will expire before the connection tries to be initialized again or make the user wait to try to start the connection again?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I am using Paperclip to handle profile photo uploads in my app. They upload
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.