If you find this article useful, consider making a small donation to show your support for this web site and its content.
Free app Developer Interview available here.

Available on the iPhone App Store
Available on the Google Play
AboutMe
About me:
Hi. My name is Farooq Kaiser and I'm a software developer from Toronto, Canada.



HTML based file upload/download to FTP server using HttpHandler in C#

by Farooq Kaiser 24. December 2009 05:18

In this article, i will examined how to upload/download file from HTML client to FTP server using HttpHandler in C#. This approach can be very handy for application integration. Here is our html code that will consume HttpHandlers.

   1: <html xmlns="http://www.w3.org/1999/xhtml" >
   2: <head>
   3:     <title>File Upload</title>
   4:       <script type="text/javascript">
   1:  
   2:       function Download() {
   3:                     document.location =  "FileDownload.ashx?Action=Jellyfish.jpg" ;    
   4:                          }
   5:         
</script>
   5: </head>
   6: <body>
   7: <form action="FileUpload.ashx" enctype="multipart/form-data" method="post">
   8:     <input type="file" name="DataFile" id="DataFile" size="40" />
   9:     <input type="submit" value="Upload" />
  10:      <input type="button" id="download" value="Download" onclick="javascript:Download();" />   
  11:     </form>
  12: </body>
  13: </html>

In next step, i will create HttpHandlers to upload/download file to FTP server.

File Upload Handler.

   1: public class FileUpload : IHttpHandler
   2:     {
   3:  
   4:         public void ProcessRequest(HttpContext context)
   5:         {
   6:  
   7:            HttpPostedFile postedFile = context.Request.Files["DataFile"];
   8:            byte[] buffer = ReadByte(postedFile.InputStream);
   9:            string url = "ftp://FTPSERVER" + @"/" + postedFile.FileName;
  10:  
  11:            FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
  12:            ftpRequest.Credentials = new NetworkCredential("UserID", "Password");
  13:            ftpRequest.Method = WebRequestMethods.Ftp.UploadFile;
  14:            
  15:            try
  16:            {
  17:                ftpRequest.GetRequestStream().Write(buffer, 0, buffer.Length);
  18:            }
  19:            catch (WebException e)
  20:            {
  21:                String status = ((FtpWebResponse)e.Response).StatusDescription;
  22:            }
  23:            
  24:         }
  25:  
  26:         public static byte[] ReadByte(Stream input) 
  27:         { 
  28:             byte[] buffer = new byte[16 * 1024]; 
  29:             using (MemoryStream ms = new MemoryStream()) 
  30:             { int read; 
  31:                 while ((read = input.Read(buffer, 0, buffer.Length)) > 0) 
  32:                 { 
  33:                     ms.Write(buffer, 0, read); 
  34:                 } 
  35:                 return ms.ToArray(); 
  36:             } 
  37:         }
  38:  
  39:  
  40:         public bool IsReusable
  41:         {
  42:             get
  43:             {
  44:                 return false;
  45:             }
  46:         }
  47:     }

File Download Handler.

   1: public class FileDownload : IHttpHandler
   2: {
   3:  
   4:       public void ProcessRequest(HttpContext context)
   5:       {
   6:           string url = "ftp://FTPSERVER" + @"/" + context.Request.QueryString["Action"];
   7:  
   8:           FtpWebRequest ftpRequest = (FtpWebRequest)FtpWebRequest.Create(new Uri(url));
   9:           ftpRequest.Credentials = new NetworkCredential("UserID", "Password");
  10:           ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
  11:           
  12:           try
  13:           {
  14:               FtpWebResponse ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
  15:               Stream stream = ftpResponse.GetResponseStream();
  16:               byte[] bytes = new byte[2048];
  17:               int byteL = 0;
  18:  
  19:               using (MemoryStream mStream = new MemoryStream())
  20:               {
  21:                   do
  22:                   {
  23:                       byteL = stream.Read(bytes, 0, bytes.Length);
  24:                       mStream.Write(bytes, 0, byteL);
  25:                   }
  26:                   while (byteL != 0);
  27:  
  28:                   context.Response.Clear();
  29:                   context.Response.ClearHeaders();
  30:                   context.Response.ClearContent();
  31:                   context.Response.ContentType = "application/octet-stream";
  32:                   context.Response.AddHeader("Content-Disposition", "attachment;filename=" + context.Request.QueryString["Action"]);
  33:                   context.Response.BinaryWrite(mStream.GetBuffer());
  34:               }
  35:           }
  36:           catch
  37:           {
  38:           }
  39:       }
  40:  
  41:       public bool IsReusable
  42:       {
  43:           get
  44:           {
  45:               return false;
  46:           }
  47:       }
  48:   }

Summary

In this article, we examined how to upload/download file from HTML client to FTP server using HttpHandler in C#.

Currently rated 2.5 by 2 people

  • Currently 2.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | C#


comments powered by Disqus

Comments

Jobs Autos Real estate Videos Power by Google