Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. file upload error

file upload error

Scheduled Pinned Locked Moved C#
csharpasp-netdesignsysadminwindows-admin
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    George_George
    wrote on last edited by
    #1

    (sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.

    Client:

       static void Main(string\[\] args)
        {
            Console.Write("\\nPlease enter the URI to post data to : ");
            String uriString = Console.ReadLine();
    
            WebClient myWebClient = new WebClient();
    
            Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI");
            string fileName = Console.ReadLine();
            Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
    
            DateTime begin = DateTime.Now;
    
            byte\[\] responseArray = null;
            try
            {
                responseArray = myWebClient.UploadFile(uriString, fileName);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.WriteLine(ex.ToString());
            }
    
            DateTime end = DateTime.Now;
    
            Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds);
        }
    

    Server:

    public partial class FileUploadHandler : System.Web.UI.Page
    {
        protected void Page\_Load(object sender, EventArgs e)
        {
            foreach (string f in Request.Files.AllKeys)
            {
                HttpPostedFile file = Request.Files\[f\];
                file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName);
            }
        }
    }
    

    Exception from client side:

    Unable to connect to the remote server
    System.Net.WebException: Unable to connect to the remote server ---> System.Net.
    Sockets.SocketException: No connection could be made because the target machine
    actively refused it 10.10.12.162:1031
    at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
    ss socketAddress)
    at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
    et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
    IAsyncResult asyncResult, Int32 timeout, Exception& exception)
    --- End of inner exception stack trace ---
    at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
    e)
    at FileUploadClien

    N H 2 Replies Last reply
    0
    • G George_George

      (sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.

      Client:

         static void Main(string\[\] args)
          {
              Console.Write("\\nPlease enter the URI to post data to : ");
              String uriString = Console.ReadLine();
      
              WebClient myWebClient = new WebClient();
      
              Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI");
              string fileName = Console.ReadLine();
              Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
      
              DateTime begin = DateTime.Now;
      
              byte\[\] responseArray = null;
              try
              {
                  responseArray = myWebClient.UploadFile(uriString, fileName);
              }
              catch (Exception ex)
              {
                  Console.WriteLine(ex.Message);
                  Console.WriteLine(ex.ToString());
              }
      
              DateTime end = DateTime.Now;
      
              Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds);
          }
      

      Server:

      public partial class FileUploadHandler : System.Web.UI.Page
      {
          protected void Page\_Load(object sender, EventArgs e)
          {
              foreach (string f in Request.Files.AllKeys)
              {
                  HttpPostedFile file = Request.Files\[f\];
                  file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName);
              }
          }
      }
      

      Exception from client side:

      Unable to connect to the remote server
      System.Net.WebException: Unable to connect to the remote server ---> System.Net.
      Sockets.SocketException: No connection could be made because the target machine
      actively refused it 10.10.12.162:1031
      at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
      ss socketAddress)
      at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
      et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
      IAsyncResult asyncResult, Int32 timeout, Exception& exception)
      --- End of inner exception stack trace ---
      at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
      e)
      at FileUploadClien

      N Offline
      N Offline
      Nicholas Butler
      wrote on last edited by
      #2

      The stack trace doesn't match the code you've posted - look at the overload of UploadFile. Anyway, one suggestion: are you putting http:// in the uriString? WebClient can use different protocols and may be getting confused. The stack trace shows a failed connection to port 1031 which is being blocked by the firewall on your server. Nick

      ---------------------------------- Be excellent to each other :)

      G 1 Reply Last reply
      0
      • N Nicholas Butler

        The stack trace doesn't match the code you've posted - look at the overload of UploadFile. Anyway, one suggestion: are you putting http:// in the uriString? WebClient can use different protocols and may be getting confused. The stack trace shows a failed connection to port 1031 which is being blocked by the firewall on your server. Nick

        ---------------------------------- Be excellent to each other :)

        G Offline
        G Offline
        George_George
        wrote on last edited by
        #3

        Thanks Nick, I have tried adding http:// is not working. I have posted my stack error and my code again. Hope it matches. :-) Any ideas what is wrong?

        class Program
        {
            static void Main(string\[\] args)
            {
                Console.Write("\\nPlease enter the URI to post data to : ");
                String uriString = Console.ReadLine();
        
                WebClient myWebClient = new WebClient();
        
                Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI");
                string fileName = Console.ReadLine();
                Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
        
                DateTime begin = DateTime.Now;
        
                byte\[\] responseArray = null;
                try
                {
                    responseArray = myWebClient.UploadFile(uriString, fileName);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.ToString());
                }
        
                DateTime end = DateTime.Now;
        
                Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds);
            }
        }
        

        Unable to connect to the remote server
        System.Net.WebException: Unable to connect to the remote server ---> System.Net.
        Sockets.SocketException: No connection could be made because the target machine
        actively refused it 10.10.12.162:1031
        at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
        ss socketAddress)
        at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
        et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
        IAsyncResult asyncResult, Int32 timeout, Exception& exception)
        --- End of inner exception stack trace ---
        at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
        e)
        at FileUploadClient.Program.Main(String[] args) in C:\software\UploadFile\Net
        work_Monitor_Demo\FileUploadClient\Program.cs:line 27
        Elapsed time is: 4579

        regards, George

        N 1 Reply Last reply
        0
        • G George_George

          Thanks Nick, I have tried adding http:// is not working. I have posted my stack error and my code again. Hope it matches. :-) Any ideas what is wrong?

          class Program
          {
              static void Main(string\[\] args)
              {
                  Console.Write("\\nPlease enter the URI to post data to : ");
                  String uriString = Console.ReadLine();
          
                  WebClient myWebClient = new WebClient();
          
                  Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI");
                  string fileName = Console.ReadLine();
                  Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
          
                  DateTime begin = DateTime.Now;
          
                  byte\[\] responseArray = null;
                  try
                  {
                      responseArray = myWebClient.UploadFile(uriString, fileName);
                  }
                  catch (Exception ex)
                  {
                      Console.WriteLine(ex.Message);
                      Console.WriteLine(ex.ToString());
                  }
          
                  DateTime end = DateTime.Now;
          
                  Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds);
              }
          }
          

          Unable to connect to the remote server
          System.Net.WebException: Unable to connect to the remote server ---> System.Net.
          Sockets.SocketException: No connection could be made because the target machine
          actively refused it 10.10.12.162:1031
          at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
          ss socketAddress)
          at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
          et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
          IAsyncResult asyncResult, Int32 timeout, Exception& exception)
          --- End of inner exception stack trace ---
          at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
          e)
          at FileUploadClient.Program.Main(String[] args) in C:\software\UploadFile\Net
          work_Monitor_Demo\FileUploadClient\Program.cs:line 27
          Elapsed time is: 4579

          regards, George

          N Offline
          N Offline
          Nicholas Butler
          wrote on last edited by
          #4

          The stack trace is still a bit off - are you using a Release build? You could try a Debug build to help track this down. I don't understand why WebClient is trying to connect on port 1031. It should be using an HTTP POST on port 80. Could you catch the WebException explicitly and print the Status code? Also, 4.5 seconds is a long time accross a LAN. How big is the file you are sending? IIS has a limit of 4MB, but you can set this using MaxRequestLength[^] in your web.config:

          <system.web>
          <httpRuntime maxRequestLength="10240" />

          If none of that works, you could try using UploadFileAsync and hooking the UploadProgressChanged event to see if anything is being sent. Nick

          ---------------------------------- Be excellent to each other :)

          G 1 Reply Last reply
          0
          • N Nicholas Butler

            The stack trace is still a bit off - are you using a Release build? You could try a Debug build to help track this down. I don't understand why WebClient is trying to connect on port 1031. It should be using an HTTP POST on port 80. Could you catch the WebException explicitly and print the Status code? Also, 4.5 seconds is a long time accross a LAN. How big is the file you are sending? IIS has a limit of 4MB, but you can set this using MaxRequestLength[^] in your web.config:

            <system.web>
            <httpRuntime maxRequestLength="10240" />

            If none of that works, you could try using UploadFileAsync and hooking the UploadProgressChanged event to see if anything is being sent. Nick

            ---------------------------------- Be excellent to each other :)

            G Offline
            G Offline
            George_George
            wrote on last edited by
            #5

            Hi Nick! Thanks for so many good ideas! I have followed your advice and here is my results. 1. The port 1031 is the Visual Studio's internal web server address port number -- i.e. when I press F5 in Visual Studio 2008 for the ASP.Net project. I have tried when I publish the ASP.Net project at server side to IIS and using port 80, everything is fine, but when pressing F5 to run in Visual Studio, there is such error. Any ideas? 2. I have tracked the Status property for the WebException, the value is "ConnectFailure". Any ideas? 3. The file is very small, about 1k and is a text file. From my 3 points above, do you have any ideas why it fails to use Visual Studio internal web server and success to use IIS? regards, George

            N 1 Reply Last reply
            0
            • G George_George

              Hi Nick! Thanks for so many good ideas! I have followed your advice and here is my results. 1. The port 1031 is the Visual Studio's internal web server address port number -- i.e. when I press F5 in Visual Studio 2008 for the ASP.Net project. I have tried when I publish the ASP.Net project at server side to IIS and using port 80, everything is fine, but when pressing F5 to run in Visual Studio, there is such error. Any ideas? 2. I have tracked the Status property for the WebException, the value is "ConnectFailure". Any ideas? 3. The file is very small, about 1k and is a text file. From my 3 points above, do you have any ideas why it fails to use Visual Studio internal web server and success to use IIS? regards, George

              N Offline
              N Offline
              Nicholas Butler
              wrote on last edited by
              #6

              So, you've been entering "http://10.10.12.162:1031" for uriString ??? This will be blocked by the firewall on the machine running VS. This means that if you run the client on the same machine, it will work because you're not crossing the firewall. If you run the client on a different machine, it will try to go through the firewall on port 1031 and ( quite rightly ) be blocked => ConnectFailure. It works on IIS using port 80 because this port is open on that machine's firewall - port 80 is the well known port for HTTP. Your code is fine - it's a network admin problem. Nick

              ---------------------------------- Be excellent to each other :)

              1 Reply Last reply
              0
              • G George_George

                (sorry I ask again, not resolved for a week before.) Hello everyone, Here is my code at both client side and server side. My code is simple, just upload a file to an ASP.Net web site. My client code throws exception when it works on Vista (x64, Enterprise, SP1), but works fine on Windows Server 2003. Any ideas? 10.10.12.162 is my server address.

                Client:

                   static void Main(string\[\] args)
                    {
                        Console.Write("\\nPlease enter the URI to post data to : ");
                        String uriString = Console.ReadLine();
                
                        WebClient myWebClient = new WebClient();
                
                        Console.WriteLine("\\nPlease enter the fully qualified path of the file to be uploaded to the URI");
                        string fileName = Console.ReadLine();
                        Console.WriteLine("Uploading {0} to {1} ...", fileName, uriString);
                
                        DateTime begin = DateTime.Now;
                
                        byte\[\] responseArray = null;
                        try
                        {
                            responseArray = myWebClient.UploadFile(uriString, fileName);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine(ex.Message);
                            Console.WriteLine(ex.ToString());
                        }
                
                        DateTime end = DateTime.Now;
                
                        Console.WriteLine("Elapsed time is: {0}", (end - begin).TotalMilliseconds);
                    }
                

                Server:

                public partial class FileUploadHandler : System.Web.UI.Page
                {
                    protected void Page\_Load(object sender, EventArgs e)
                    {
                        foreach (string f in Request.Files.AllKeys)
                        {
                            HttpPostedFile file = Request.Files\[f\];
                            file.SaveAs("D:\\\\UploadFile\\\\UploadedFiles\\\\" + file.FileName);
                        }
                    }
                }
                

                Exception from client side:

                Unable to connect to the remote server
                System.Net.WebException: Unable to connect to the remote server ---> System.Net.
                Sockets.SocketException: No connection could be made because the target machine
                actively refused it 10.10.12.162:1031
                at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddre
                ss socketAddress)
                at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Sock
                et s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state,
                IAsyncResult asyncResult, Int32 timeout, Exception& exception)
                --- End of inner exception stack trace ---
                at System.Net.WebClient.UploadFile(Uri address, String method, String fileNam
                e)
                at FileUploadClien

                H Offline
                H Offline
                Hamid Taebi
                wrote on last edited by
                #7

                And see File Uploading in ASP.NET Using C#[^] for more info. :)

                Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups