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. WebClient.UploadFile won't upload the file.

WebClient.UploadFile won't upload the file.

Scheduled Pinned Locked Moved C#
csharpdatabasehelpphpmysql
4 Posts 2 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.
  • U Offline
    U Offline
    User 4414712
    wrote on last edited by
    #1

    Hello. Im having a php post site on my server where i can upload files. The problem is just that it doest upload the file at all. Im trying through the php script to upload a file to the database. It works fine when i do it manually from the site, but not when im using UploadFile(). My C# code looks like: string file = "1.jpg"; string url = "http://myserver.net/ind.php?userid="+\_userid+""; try { WebClient InsertClient = new WebClient(); byte[] Response = InsertClient.UploadFile(url, "POST", file); label1.Text = Response.Length.ToString(); } catch (Exception ex) { label1.Text = ex.ToString(); } And the uploadsite looks like and is called "ind.php": $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include "dbcon.php"; $query = "INSERT INTO files (userid, imageData ) ". "VALUES ('".$_GET['userid']."', '$content')"; mysql_query($query) or die('Error, query failed'); echo "
    File $fileName uploaded
    "; It only adds the userid to the database but without any content of the file. Have anyone got this problem before?

    U J 3 Replies Last reply
    0
    • U User 4414712

      Hello. Im having a php post site on my server where i can upload files. The problem is just that it doest upload the file at all. Im trying through the php script to upload a file to the database. It works fine when i do it manually from the site, but not when im using UploadFile(). My C# code looks like: string file = "1.jpg"; string url = "http://myserver.net/ind.php?userid="+\_userid+""; try { WebClient InsertClient = new WebClient(); byte[] Response = InsertClient.UploadFile(url, "POST", file); label1.Text = Response.Length.ToString(); } catch (Exception ex) { label1.Text = ex.ToString(); } And the uploadsite looks like and is called "ind.php": $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include "dbcon.php"; $query = "INSERT INTO files (userid, imageData ) ". "VALUES ('".$_GET['userid']."', '$content')"; mysql_query($query) or die('Error, query failed'); echo "
      File $fileName uploaded
      "; It only adds the userid to the database but without any content of the file. Have anyone got this problem before?

      U Offline
      U Offline
      User 4414712
      wrote on last edited by
      #2

      And yes, the upload form is a POST method :)

      1 Reply Last reply
      0
      • U User 4414712

        Hello. Im having a php post site on my server where i can upload files. The problem is just that it doest upload the file at all. Im trying through the php script to upload a file to the database. It works fine when i do it manually from the site, but not when im using UploadFile(). My C# code looks like: string file = "1.jpg"; string url = "http://myserver.net/ind.php?userid="+\_userid+""; try { WebClient InsertClient = new WebClient(); byte[] Response = InsertClient.UploadFile(url, "POST", file); label1.Text = Response.Length.ToString(); } catch (Exception ex) { label1.Text = ex.ToString(); } And the uploadsite looks like and is called "ind.php": $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include "dbcon.php"; $query = "INSERT INTO files (userid, imageData ) ". "VALUES ('".$_GET['userid']."', '$content')"; mysql_query($query) or die('Error, query failed'); echo "
        File $fileName uploaded
        "; It only adds the userid to the database but without any content of the file. Have anyone got this problem before?

        U Offline
        U Offline
        User 4414712
        wrote on last edited by
        #3

        And the output from the upload site is that "File name cannot be empty"..

        1 Reply Last reply
        0
        • U User 4414712

          Hello. Im having a php post site on my server where i can upload files. The problem is just that it doest upload the file at all. Im trying through the php script to upload a file to the database. It works fine when i do it manually from the site, but not when im using UploadFile(). My C# code looks like: string file = "1.jpg"; string url = "http://myserver.net/ind.php?userid="+\_userid+""; try { WebClient InsertClient = new WebClient(); byte[] Response = InsertClient.UploadFile(url, "POST", file); label1.Text = Response.Length.ToString(); } catch (Exception ex) { label1.Text = ex.ToString(); } And the uploadsite looks like and is called "ind.php": $fileName = $_FILES['userfile']['name']; $tmpName = $_FILES['userfile']['tmp_name']; $fileSize = $_FILES['userfile']['size']; $fileType = $_FILES['userfile']['type']; $fp = fopen($tmpName, 'r'); $content = fread($fp, filesize($tmpName)); $content = addslashes($content); fclose($fp); if(!get_magic_quotes_gpc()) { $fileName = addslashes($fileName); } include "dbcon.php"; $query = "INSERT INTO files (userid, imageData ) ". "VALUES ('".$_GET['userid']."', '$content')"; mysql_query($query) or die('Error, query failed'); echo "
          File $fileName uploaded
          "; It only adds the userid to the database but without any content of the file. Have anyone got this problem before?

          J Offline
          J Offline
          jo H
          wrote on last edited by
          #4

          Your problem is with your PHP code which is looking at the wrong variables. The upload method places the info into $_FILES['file'], not $_FILES['userfile']. Better late than never I suppose.

          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