Contents of the Series

Subscribe

Subscribe to get notified when new series or atleast 5 new tutorials are released:

Email:




Uploading pdf, doc, and xls files over FTP Server



Download ScreenShots
Download Code Sample
Introduction

In this tutorial we will disucss how to upload pdf, doc, xls and any other type of files over ftp server using stream, FtpWebRequest classes and asp.net file upload control. If you have been searching for this solution then I assume that you might have come across many tutorials about ftp upload, which do work but when you upload them to production server they broke. Therefore I have written this tutorial which also works on production.

We will use the following controls, classes and nampespaces in this step by step tutorial;

  • System.IO Namespace
  • System.IO.Stream Class
  • System.Net Namespace
  • System.Net.FtpWebRequest Class
  • System.Net.NetworkCredential Class
  • System.Net.WebRequestMethods Class
  • FileUpload Control
Screen Shots

This time rather than putting images between the tutorials I have attached the tuturial screenshots.

Understanding Streams

Steps
  1. Create a new Web Site
  2. Add a file upload, label and button controls to default.aspx
  3. (We will upload file using a page postback, so you don't need to add a click event on a button)
    Go to code file and add the following code in the page load event, to check whether if this is a post back or not;

       if(IsPostback)
       {
       }
  4. Now check whether any file is uploaded or not using ContentUpload.HasFile property;

       if(IsPostback)
       {
          if (ContentUpload.HasFile)
          {
          }
       }
  5. Before proceeding further make sure you have included System.Nampespace in your project.
  6. We will definitely upload a file on some ftp server, so we need a uri of that server plus we will also add a file name to the uri, which will be the file name for the remote location.


       string remoteFileName = file.FileName;
       string uri = "ftp://ftp.themastech.net/" + remoteFilename;


    If you want to store file in a directory then do it like this;


       string uri = "ftp://ftp.themastech.net/Directory/" + remoteFilename;


  7. Now we need to create a FtpWebRequest object. The FtpWebRequest class provides the ability to programmatically interact with FTP servers. To create FtpWebRequest object use its Create method which takes uri as a parameter.


       FtpWebRequest reqFTP;
       reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));


  8. You must have a valid user name and password for the server or the server must allow anonymous logon. You must have WebPermission to access the FTP resource; otherwise, a SecurityException exception is thrown. To provide the the credentials to the ftp request you must provide FtpWebRequest.Credentials property by creating a new NetworkCredentials object.


       reqFTP.Credentials = new NetworkCredential("username", "password");
     
    Caution:

    Unless the EnableSsl property is true, all data and commands, including your user name and password information, are sent to the server in clear text. Anyone monitoring network traffic can view your credentials and use them to connect to the server. If you are connecting to an FTP server that requires credentials and supports Secure Sockets Layer (SSL), you should set EnableSsl to true.

  9. Since we want to upload a file to the ftp server we need to provide a method property to FtpWebRequest object;


       reqFTP.Method = WebRequestMethods.Ftp.UploadFile;


    This way you tell the Ftp request that you want to upload the file.
  10. Before proceeding further make sure you have included System.IO namesapce in your project.
  11. File will be uploaded using stream object. FtpWebRequest object provides a method GetRequestStream() which retrieves the stream used to upload the data to an FTP server.


       Stream OutputStream = reqFTP.GetRequestStream();


  12. Our file, which we want to upload will be our InputStream.


       Stream InputStream = file.InputStream;


    This will make the file ready to read. That's it. Now we just need to copy the contents from InputStream to OutputStream.
  13. With stream read, the code now, should look like this; protected

       void Page_Load(object sender, EventArgs e)
       {
          if (IsPostBack)
          {
             if (ContentUpload.HasFile)
             {
                string remoteFilename = ContentUpload.FileName;

                string uri = "ftp://ftp.themastech.net/" + remoteFilename;
                FtpWebRequest reqFTP; reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
                reqFTP.Credentials = new NetworkCredential("username", "password");
                reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
                Stream OutputStream = reqFTP.GetRequestStream();
                Stream InputStream = file.InputStream;

                int buffLength = 2048;
                byte[] buff = new byte[buffLength];
                int contentLen = (int)file.InputStream.Length;

                while (contentLen != 0)
                {
                   OutputStream.Write(buff, 0, contentLen);
                   contentLen = InputStream.Read(buff, 0, buffLength);
                }

                OutputStream.Close();
                InputStream.Close();

                MessageLabel.Text = "File Uploaded";
             }
          }
       }
Conclusion

In this tutorial we disucusssed how to upload different type of files over ftp server using FtpWebRequest and Stream classes. To downlad files you just create a link with reference to that file e.g, <a href="http://www.themastech.net/filename.xls">Download File</a>.

References:

http://technet.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx
http://www.windowsdevcenter.com/pub/a/windows/2006/12/12/building-ftp-services-using-net-20.html?page=last






Tutorial Request:

Which tutorial or a series of tutorials, on ASP.Net 2.0 or dotNet 2, you would like to see in the future, or which you think are not avaialble in a good informative form?


If possible, do provide some links too, especially from msdn liabrary.


Name:
Email:
Title:

Message: