FtpWebRequest locks up web applications on server

Posted on 2009-07-15
Last Modified: 2013-11-29
I think my code is locking up the server when using FtpWebRequest to upload a large file to a remote server. The attached code is run inside an ASP.NET application.  Could someone please review the code and let me know if I am making any obvious mistakes?

What is happening is that the ASP.NET application that this code is part of ends up locking up another, non-ASP.NET application that runs in a separate thread.  By locking up, I mean that if I open:

which runs the code below and, at the same time I run: (runs via a separate ISAPI extension)

then the second application does not respond until the file in the first application has been uploaded.

Please let me know if I am doing something wrong in how I use FtpWebRequest or how I could troubleshoot this...  :\

private void Upload(string filename, string ftpServer, string ftpUserID, string ftpPassword)


            FileInfo fileInf = new FileInfo(filename);

            string uri = "ftp://" + ftpServer + "/" + fileInf.Name;

            FtpWebRequest reqFTP;

            // Create FtpWebRequest object from the Uri provided

            reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServer + "/" + fileInf.Name));

            // Provide the WebPermission Credintials

            reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

            // By default KeepAlive is true, where the control connection is not closed

            // after a command is executed.

            reqFTP.KeepAlive = false;

            // Specify the command to be executed.

            reqFTP.Method = WebRequestMethods.Ftp.UploadFile;

            // Specify the data transfer type.

            reqFTP.UseBinary = true;

            // Notify the server about the size of the uploaded file

            reqFTP.ContentLength = fileInf.Length;

            // The buffer size is set to 2kb

            int buffLength = 2048;

            byte[] buff = new byte[buffLength];

            int contentLen;

            // Opens a file stream (System.IO.FileStream) to read the file to be uploaded

            FileStream fs = fileInf.OpenRead();



                // Stream to which the file to be upload is written

                Stream strm = reqFTP.GetRequestStream();

                // Read from the file stream 2kb at a time

                contentLen = fs.Read(buff, 0, buffLength);

                // Till Stream content ends


                while (contentLen != 0)


                    // Write Content from the file stream to the FTP Upload Stream

                    strm.Write(buff, 0, contentLen);

                    contentLen = fs.Read(buff, 0, buffLength);


                // Close the file stream and the Request Stream




            catch (Exception ex)


                Response.Write("<p><font color='red'>Error Uploading File</font></p>");



Open in new window

Question by:Akitin
  • 2

Expert Comment

by:Rahul Goel
ID: 24866926
I thnk you must use the windows service or DTS/SSIS to do that. Using web application you can put that file in the specific folder and intimate the user that file has been marked to upload and it will upload in next 30 mins.  and windows service or dts could be fired to do operation.
That's the best approach I must suggest you

Author Comment

ID: 24870080
Thanks for that Rahu, but I would like to try to figure out why this is not working as I would like to display confirmation of successful upload.  Also, I'd like to understand why this is happening so that I can make sure that I take all precautions necessary in my applications to prevent lockups.

Accepted Solution

Akitin earned 0 total points
ID: 24961418

After some more research, I found that it might not be the FTP code locking up the server and perhaps that code was written correctly.


Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Data Saving(2) 2 28
cs.Designer Issue 2 20
XML & .net 5 21
Open a word document 23 19
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
After creating this article (, I decided to make a video (no audio) to show you how to configure the routers and run some trace routes and pings between the 7 sites…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now