Click here to Skip to main content
15,911,786 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I create a file uploader in ASP.NET using C# but when I upload large file it takes a long time to upload

How I speed it up?

What I have tried:

protected void Button1_Click(object sender, EventArgs e)
   {
       if (FileUpload1.HasFiles)
       {
           foreach (HttpPostedFile postedfile in FileUpload1.PostedFiles)
           {
               byte[] img = new byte[postedfile.ContentLength];
               postedfile.InputStream.Read(img, 0, postedfile.ContentLength);

               string file_ext = postedfile.FileName;
               string ext = Path.GetExtension(file_ext);

               // ftp
               if ((ext.ToLower() == ".jpg") || (ext.ToLower() == ".jpeg") || (ext.ToLower() == ".gif") || (ext.ToLower() == ".png"))
               {
                   Random rnd = new Random();
                   int card = rnd.Next();
                   string location_filename = "/Album/" + Album_Id + "/" + card + "_" + file_ext;
                   System.Windows.Forms.MessageBox.Show(location_filename);

                   MemoryStream stream = new MemoryStream(img);
                   System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
                   if ((image.Width > 250 && image.Width < 3650) && (image.Height > 250) && image.Height < 4450)
                   {
                       ftp_class.UploadFileToFTP(img, location_filename);
                   }
                   else
                   {
                       // Label1.Text = "Image is To Large";

                   }
                   //    File.WriteAllBytes(Server.MapPath("~/Property/") + Property_ID + "/" + card + file_ext, img);
               }
           }
       }


public class ftp_class
   {
       static String ftpusername = "Ftp_ExampleCodeExample"; // e.g. username
       static String ftppassword = "Example@123"; // e.g. password


       public static void CreateDirectoryToFTP(string directory)
       {
           try
           {
              // Directory.CreateDirectory(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "/" + directory);



               FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://example.com/" + directory);
               ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
               ftp.KeepAlive = true;
               ftp.UseBinary = true;
               ftp.Method = WebRequestMethods.Ftp.MakeDirectory;
               FtpWebResponse responseFileDelete = (FtpWebResponse)ftp.GetResponse();

           }
           catch (Exception ex)
           {
               //  alert_message.alert_message_show("ftp:" + ex);
           }
       }
       public static void UploadFileToFTP(byte[] img, string location_filename)
       {
           try
           {
             //  File.WriteAllBytes(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + "/" + location_filename, img);


               FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create("ftp://example.com/" + location_filename);
               ftp.Credentials = new NetworkCredential(ftpusername, ftppassword);
               ftp.KeepAlive = true;
               ftp.UseBinary = true;
               ftp.Method = WebRequestMethods.Ftp.UploadFile;
               Stream ftpstream = ftp.GetRequestStream();
               ftpstream.Write(img, 0, img.Length);
               ftpstream.Close();

           }
           catch (Exception ex)
           {
               //extra.alter_box("" + ex);
               //   alert_message.alert_message_show("ftp:" + ex);
           }
       }

   }
Posted
Updated 4-Mar-18 13:54pm
v2

1 solution

Get a faster client internet connection.

Uploads are always slower than downloads, typically 1/5 the speed at best. And large files take a lot of bandwidth.
Check the size of your file, check your upload speed and start by working out the best time you could get by
upload speed / (file bytes * 10) == seconds to transfer
- that';ll give you an approximate "best case" transfer, in practice it'll almost certainly be slower than that.

If your actual time is similar (i.e. between the best rate time and twice that) then you really can't improve upload without changing your connection.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900