0

I'm trying to write a tool to batch upload images to my website, but I'm having trouble receiving (or sending) the actual data to the server.

I'll start with some C# code as it should explain what I'm trying to do better that I can articulate:

private bool Upload( string LocalFile, int ItemID, string Description, DateTime Date, string Photographer )
    {
        WebClient oWeb = new System.Net.WebClient(); 
        NameValueCollection parameters = new NameValueCollection();
        parameters.Add( "Type", "1" );
        parameters.Add( "ID", ItemID.ToString() );
        parameters.Add( "Desc", Description );
        parameters.Add( "Date", Date.ToString( "yyyy-MM-dd" ) );
        parameters.Add( "Photographer", Photographer );

        oWeb.QueryString = parameters;
        var responseBytes = oWeb.UploadFile( "http://www.teamdefiant.co.uk/moveuploadedfile.php", LocalFile );
        string response = Encoding.ASCII.GetString(responseBytes);

        MessageBox.Show( "Response: \n\n" + response + "\n\nPost Data: \n\n" + LocalFile + "\n" + ItemID.ToString() + "\n" + Description + "\n" + Date.ToString("yyyy-MM-dd") + "\n" + Photographer );
        Clipboard.SetText( "Response: \n\n" + response + "\n\nPost Data: \n\n" + LocalFile + "\n" + ItemID.ToString() + "\n" + Description + "\n" + Date.ToString("yyyy-MM-dd") + "\n" + Photographer );

        return true;
    }

When I receive the response, (as displayed in the MessageBox, and Clipboard.SetText) I get the following data:

Response:

Upload:
Type:
Size: 0kb
Stored in:







Post Data:

C:\Users\<MyFolder>\Pictures\Website\ExampleImage.png
4
Picture Description
2014-12-10
Photographer

And for completeness, here's the php code:

<?PHP
   if ($_FILES["myfile"]["error"] > 0)
   {
      echo "Error: " . $_FILES["myfile"]["error"] . "\n";
   }
   else
   {
      echo "Upload: " . $_FILES["myfile"]["name"] . "\n";
      echo "Type: " . $_FILES["myfile"]["type"] . "\n";
      echo "Size: " . ($_FILES["myfile"]["size"] / 1024) . " Kb\n";
      echo "Stored in: " . $_FILES["myfile"]["tmp_name"] . " \n";
      echo $_POST["Type"] . " \n";
      echo $_POST["ID"] . " \n";
      echo $_POST["Desc"] . " \n";
      echo $_POST["Date"] . " \n";
      echo $_POST["Photographer"] . " \n";
   }
?> 

I've tried searching for answers but haven't been able to find a working solution that I can understand. (I'm still just learning C#.)

I don't get any code errors in Visual Studio, not do I get any POST errors server side, so I'm stumped.

Any and all help is appreciated!

EDIT

I tried visiting the webpage directly and get the same response, so it looks like no data is being sent to the server?? :(

Philip Lewis
  • 57
  • 2
  • 9
  • where is your form code, sometimes not putting multipart and other things on the
    tag could cause the problem.
    – unixmiah Dec 10 '14 at 22:07
  • What form tag? I'm not uploading any data from a web form. I'm trying to upload the data programatically from my C# app. – Philip Lewis Dec 10 '14 at 22:09
  • on the php code add print_r($_POST); – unixmiah Dec 10 '14 at 22:11
  • Sorry for the delay, I went to bed shortly after posting this question and was at work all day. I added print_r( $_POST ); (In fact, I made it the only line in the file.) and this is the response I got: Array ( ) – Philip Lewis Dec 11 '14 at 17:59

1 Answers1

0

your c# front-end code doesn't have few things

you need to define a content type octet stream

Client.Headers.Add("Content-Type","binary/octet-stream");

you also need to specify POST in your UploadFile

oWeb.UploadFile ("http://www.teamdefiant.co.uk/moveuploadedfile.php","POST", LocalFile);

try this simple code I found on the internet, create a different work branch and give it a try, this is a lot more basic and in a working condition:

the php script:

<?php
$uploaddir = 'upload/'; // Relative Upload Location of data file

if (is_uploaded_file($_FILES['file']['tmp_name'])) {
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
echo "File ". $_FILES['file']['name'] ." uploaded successfully. ";


if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully moved. ";
}

else
print_r($_FILES);
}

else {
echo "Upload Failed!!!";
print_r($_FILES);
}
?>

and this is the C# code:

System.Net.WebClient Client = new System.Net.WebClient ();
Client.Headers.Add("Content-Type","binary/octet-stream");
byte[] result = Client.UploadFile ("http://your_server/upload.php","POST","C:\test.jpg");
string s = System.Text.Encoding .UTF8 .GetString (result,0,result.Length );
unixmiah
  • 3,081
  • 1
  • 12
  • 26
  • Thanks for your answer. If I have the following lines, I still get blank $_POST data. oWeb.QueryString = parameters; oWeb.Headers.Add("Content-Type", "binary/octet-stream"); var responseBytes = oWeb.UploadFile( "http://www.teamdefiant.co.uk/moveuploadedfile.php", "POST", LocalFile ); If I have the lines this way around, I get an exception error; oWeb.Headers.Add("Content-Type", "binary/octet-stream"); oWeb.QueryString = parameters; var responseBytes = oWeb.UploadFile( "http://www.teamdefiant.co.uk/moveuploadedfile.php", "POST", LocalFile ); – Philip Lewis Dec 11 '14 at 18:31
  • It threw an exception. :( – Philip Lewis Dec 11 '14 at 18:48
  • try putting in "C:\image.jpg" in the 3rd param, make sure you test out an image – unixmiah Dec 11 '14 at 18:48
  • try this client.UploadFile("http://www.teamdefiant.co.uk/moveuploadedfile.php", "POST", @"C:\testimage.jpg"); – unixmiah Dec 11 '14 at 18:51
  • Tried both @"c:\image.png" as well as "c:\\image.png", both provided a blank post array again. – Philip Lewis Dec 11 '14 at 19:06
  • This works, but obviously no image gets uploaded. `var responseBytes = oWeb.UploadValues( "http://www.teamdefiant.co.uk/moveuploadedfile.php", "POST", parameters );` – Philip Lewis Dec 11 '14 at 19:40
  • that is really strange – unixmiah Dec 11 '14 at 19:54
  • I also tried the following, again got a blank POST array... `byte[] bytes = System.IO.File.ReadAllBytes( LocalFile );` `var responseBytes = oWeb.UploadData( "http://www.teamdefiant.co.uk/moveuploadedfile.php", "POST", bytes );` – Philip Lewis Dec 11 '14 at 19:59
  • @unixmiah's method will only put the data in the query string. There is no native way to put it in the `$_POST` with `WebClient`. See this answer for a class to what you want http://stackoverflow.com/a/11048296/740639. – Walter Stabosz Dec 31 '14 at 22:59