Uploading files to FTP server using ASP.Net

Uploading files using FileUpload server control is very common and easy with ASP.Net. But normally in this method files are uploaded to the hard disk of the web server. Here are few articles on this:

But what if we want to upload the files directly to some FTP server instead of web server. This article show how to use same FileUpload server control of ASP.Net to upload files directly to some other FTP server.

Below is the function written in C# which takes HttpPostedFile object as parameters and uploads the associated file to FTP server ftp://demo.myFtpServer.com/UploadFles

 

 

private bool UploadToFTP(HttpPostedFile fileToUpload)
       {

           try
           {
               string uploadUrl = @"ftp://demo.myFtpServer.com/UploadFles";
               string uploadFileName = fileToUpload.FileName;

               Stream streamObj = fileToUpload.InputStream;
               Byte[] buffer = new Byte[fileToUpload.ContentLength];
               streamObj.Read(buffer, 0, buffer.Length);
               streamObj.Close();
               streamObj = null;

               string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
               FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
               requestObj.Method = WebRequestMethods.Ftp.UploadFile;
               requestObj.Credentials = new NetworkCredential("userid", "password");
               Stream requestStream = requestObj.GetRequestStream();
               requestStream.Write(buffer, 0, buffer.Length);
               requestStream.Flush();
               requestStream.Close();
               requestObj = null;

               return true;
           }
           catch
           {
               return false;
           }
       }

Additional links:

17 comments

  1. This code is not working in asp.net
    http;//www.spiritssoft.com

    ReplyDelete
  2. This code is currently working for me. Can I know what error message you are getting. And please ensure you have proper credentials for the FTP location you are uploading.

    ReplyDelete
  3. The following error occurred when used the above code, could you please help me out to resolve the issue...

    The remote server returned an error: (501) Syntax error in parameters or arguments.

    ReplyDelete
  4. thanks a lot. You save my time.

    ReplyDelete
  5. pinoooooooooooooooooooooooooooooooooooo .. that code works for me as well... thanks ..

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

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

    ReplyDelete
  8. Wow.....! Code works perfectly

    It saved my hours!

    Thanks....!

    ReplyDelete
  9. Perfect one. Saved lot of time. Thank you

    ReplyDelete
  10. super post.. works awesome.. include

    requestObj.Proxy = null;
    to avoid http post error

    ReplyDelete
  11. Excellent ........ Working 100%

    ReplyDelete
  12. thanks very much
    but this code uploaded only kb file.
    i want to be upload file in MB,GB or in TB.
    then ho i can do it...Plz reply....
    again Thanks......

    ReplyDelete

Posts a comment

Popular Posts

 
© Old - Pinal Bhatt's Blog
From the desk of Pinal Bhatt | www.PBDesk.com
Back to top