Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I found the following methods and found that the remote upload and download speed is slow, often abnormal, hope expert guidance.
C#
public static bool UploadFile(string localFilePath, string serverFolder)
       {
           bool b = false;
           System.Net.WebClient myWebClient = new System.Net.WebClient();
           myWebClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
           FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);
           BinaryReader br = new BinaryReader(fs);

          byte[] postArray = br.ReadBytes((int)fs.Length);
          Stream postStream = myWebClient.OpenWrite(serverFolder, "PUT");
          if (postStream.CanWrite)
               {
                   postStream.Write(postArray, 0, postArray.Length);
                   b = true;
               }
               postStream.Close();
               postStream.Dispose();



           return b;
       }

          public static string Download(string uri, string savePath)
       {
           if (!Directory.Exists(savePath)) Directory.CreateDirectory(savePath);

           string fileName = string.Empty;

           if (uri.IndexOf(@"..\") > -1 || uri.IndexOf(@"\") > -1)
           {
               uri = uri.Replace(@"..\", "/");
               uri = uri.Replace(@"\", "/");
           }
           if (uri.IndexOf("\\") > -1)
           {
               fileName = uri.Substring(uri.LastIndexOf("\\") + 1);
           }
           else
           {
               fileName = uri.Substring(uri.LastIndexOf("/") + 1);
           }
           if (!savePath.EndsWith("/") && !savePath.EndsWith("\\"))
           {
               savePath = savePath + "/";
           }


           WebClient client = new WebClient();

               client.DownloadFile(uri, savePath);



           return savePath;
       }
Posted
Comments
Sergey Alexandrovich Kryukov 24-Sep-13 21:39pm    
Why do you think it's slow? Did you compare with the speed of Web browsers downloading from the same URLs? The throughput depends not only on your code, but main bottleneck is usually the network traffic and the server, which can be busy.
—SA
open3820000 24-Sep-13 21:41pm    
Yes ,upload files with a browser is faster and does not abnormal
BillWoodruff 25-Sep-13 1:44am    
What type of files are you uploading to what type of server ?
open3820000 25-Sep-13 6:08am    
Any file, as long as there will be problems remotely.

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