WebClient.UploadFile problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Svinja
    New Member
    • Nov 2007
    • 25

    WebClient.UploadFile problem

    Hi, i am using WebClient.Uploa dFile to upload a file on my web page, code:
    Code:
    WebClient webClient = new WebClient();
    webClient.Credentials = new NetworkCredential(userName, pass);
    webClient.UploadFile("http://www.etfos.hr/~dhuis/test.txt", @"c:\test.txt");
    i get an error "The remote server returned an error: (404) FIle not found"
    my web page is http://www.etfos.hr/~dhuis
    permissions are all enabled
    i tried with httpwebrequest class and i get the same error
    i can upload with ftpwebrequest but then it is slow.
    Any suggestions?
  • Svinja
    New Member
    • Nov 2007
    • 25

    #2
    Do i have to make some asp/php code to recieve it on my server? I thought http will do it for me like ftp does?

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Originally posted by Svinja
      Hi, i am using WebClient.Uploa dFile to upload a file on my web page, code:
      Code:
      WebClient webClient = new WebClient();
      webClient.Credentials = new NetworkCredential(userName, pass);
      webClient.UploadFile("http://www.etfos.hr/~dhuis/test.txt", @"c:\test.txt");
      i get an error "The remote server returned an error: (404) FIle not found"
      my web page is http://www.etfos.hr/~dhuis
      permissions are all enabled
      i tried with httpwebrequest class and i get the same error
      i can upload with ftpwebrequest but then it is slow.
      Any suggestions?

      I'm not clear on what you are doing.
      The WebClient Class sends a local file to the resource you've specified...

      The resource you've specified is:
      "http://www.etfos.hr/~dhuis/test.txt"

      This isn't a valid URI location to upload your file to....it cannot find "http://www.etfos.hr/~dhuis/test.txt" and so you are getting a 404 error.

      Try instead:
      "http://www.etfos.hr/dhuis/"

      I'm not 100% sure this will work. But I am sure that you are using an invalid string to indicate the URL of the resource you want to upload the file to.

      See the WebClient.Uploa dFile Method for examples on how to use this.

      -Frinny

      Comment

      • Svinja
        New Member
        • Nov 2007
        • 25

        #4
        Originally posted by Frinavale
        I'm not clear on what you are doing.
        The WebClient Class sends a local file to the resource you've specified...

        The resource you've specified is:
        "http://www.etfos.hr/~dhuis/test.txt"

        This isn't a valid URI location to upload your file to....it cannot find "http://www.etfos.hr/~dhuis/test.txt" and so you are getting a 404 error.

        Try instead:
        "http://www.etfos.hr/dhuis/"

        I'm not 100% sure this will work. But I am sure that you are using an invalid string to indicate the URL of the resource you want to upload the file to.

        See the WebClient.Uploa dFile Method for examples on how to use this.

        -Frinny
        Thanks for your answer but i believe you are wrong, the uri is correct. "http://www.etfos.hr/~dhuis/test.txt" means that i want to put the file in
        "http://www.etfos.hr/~dhuis/" and name it "test.txt". I believe i should have the receiving script on my web server, i will try to make one and post it if i succeed

        Comment

        • Svinja
          New Member
          • Nov 2007
          • 25

          #5
          I was right, asp/php script is necessary:
          c# code:
          Code:
          WebClient webClient = new WebClient();
          byte []responseBytes=webClient.UploadFile("http://www.etfos.hr/~dhuis/Upload.php", @"c:\data2.dat");
          String response = Encoding.Default.GetString(responseBytes);
          MessageBox.Show(response);
          php code(Upload.php ):
          Code:
          <?php
          $uploadDir = 'Upload/'; 
          $uploadFile = $uploadDir . basename($_FILES['file']['name']);
          if (is_uploaded_file($_FILES['file']['tmp_name'])) 
          {
          	echo "File ". $_FILES['file']['name'] ." is successfully uploaded!\r\n";
          	if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) 
          	{
          		echo "File is successfully stored! ";
          	}
          	else print_r($_FILES);
          }
          else 
          {
          	echo "Upload Failed!";
          	print_r($_FILES);
          }
          ?>
          -Upload.php must be in "http://www.etfos.hr/~dhuis/"
          -Upload directory must have write permission

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Svinja
            I was right, asp/php script is necessary:
            c# code:
            Code:
            WebClient webClient = new WebClient();
            byte []responseBytes=webClient.UploadFile("http://www.etfos.hr/~dhuis/Upload.php", @"c:\data2.dat");
            String response = Encoding.Default.GetString(responseBytes);
            MessageBox.Show(response);
            php code(Upload.php ):
            Code:
            <?php
            $uploadDir = 'Upload/'; 
            $uploadFile = $uploadDir . basename($_FILES['file']['name']);
            if (is_uploaded_file($_FILES['file']['tmp_name'])) 
            {
            	echo "File ". $_FILES['file']['name'] ." is successfully uploaded!\r\n";
            	if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile)) 
            	{
            		echo "File is successfully stored! ";
            	}
            	else print_r($_FILES);
            }
            else 
            {
            	echo "Upload Failed!";
            	print_r($_FILES);
            }
            ?>
            -Upload.php must be in "http://www.etfos.hr/~dhuis/"
            -Upload directory must have write permission

            Thanks for sharing your solution :)

            -Frinny

            Comment

            Working...