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:
- ASP.NET Technical Articles : Uploading Files in ASP.NET 2.0
- Wrox : ASP.NET 2.0 FileUpload Server Control
- CodeGuru : ASP.NET 2.0 FileUpload Server Control
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:
This code is not working in asp.net
ReplyDeletehttp;//www.spiritssoft.com
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.
ReplyDeleteThe following error occurred when used the above code, could you please help me out to resolve the issue...
ReplyDeleteThe remote server returned an error: (501) Syntax error in parameters or arguments.
the code works perfect for me :) ty
ReplyDeletethanks a lot. You save my time.
ReplyDeletethank for good answer!
ReplyDeletepinoooooooooooooooooooooooooooooooooooo .. that code works for me as well... thanks ..
ReplyDeletethis is not working...The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
ReplyDeleteThe remote server returned an error: (550) File unavailable (e.g., file not found, no access).
ReplyDeleteWow.....! Code works perfectly
ReplyDeleteIt saved my hours!
Thanks....!
not working.............
ReplyDeletePerfect one. Saved lot of time. Thank you
ReplyDeletesuper post.. works awesome.. include
ReplyDeleterequestObj.Proxy = null;
to avoid http post error
From the desk of Pinal Bhatt
ReplyDeleteExcellent ........ Working 100%
ReplyDeleteGood .Code Work Perfect.
ReplyDeletethanks very much
ReplyDeletebut 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......