0

I am uploading a file with C# code on php server. But facing some issues.

First I was using a WebClient Object to upload file by calling UploadFile() method, and uploading string to by calling UploadString() method by following code:

        String StoreID = "First Store";
        WebClient Client = new WebClient();
        String s = Client.UploadString("http://localhost/upload.php", "POST", StoreID);
        Client.Headers.Add("Content-Type","binary/octet-stream");
        byte[] result = Client.UploadFile("http://localhost/upload.php", "POST", "C:\\aaaa.jpg");
        s = s + System.Text.Encoding.UTF8.GetString(result,0,result.Length);

Issue is that I am requesting two times so string and file is not being send at same time. I am receiving either String or File. But I need both at same time. I don't want to use UploadData() becuase it will use byte codes and I have know I idea how to extract it in php.

Let that string is folder name, i have to send string and file, so that file could save at specified folder at php server.

I studied there may be a solution with WebRequest and WebResponse object. But dont know how to send request using WebResponse by C# and get it at PHP.

Any Suggestions!!!!

MouseCrasher
  • 453
  • 1
  • 5
  • 14
  • See: http://stackoverflow.com/questions/2950292/how-to-upload-multiple-files-using-webclient-uploadfile-uploadvalues-in-c – Mike Caron Oct 31 '12 at 13:08

2 Answers2

0

Try this :

    WebClient web = new WebClient();
try{

    web.UploadFile("http://" + ip + "/test.php", StoreID);
}
catch(Exception e)
{
    MessageBox.Show("Upload failed");
}

Now you can access the file from the PHP file.

 <?php
//check whether the folder the exists
if(!(file_exists('C:/Users/dhanu-sdu/Desktop/test')))
{
  //create the folder
  mkdir('C:/Users/ComputerName/Desktop/test');
  //give permission to the folder
  chmod('C:/Users/ComputerName/Desktop/test', 0777);
}

//check whether the file exists
if (file_exists('C:/Users/ComputerName/Desktop/test/'. $_FILES["file"]["name"]))
{
  echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
  //move the file into the new folder
  move_uploaded_file($_FILES["file"]["tmp_name"],'C:/Users/ComputerName/Desktop/test/'. $_FILES["file"]["name"]);

}

?>

Also, you can download data from a PHP server and display it in a C# web browser by using the following codes :

 WebClient web = new WebClient();
try{
    byte[] response = web.DownloadData("http://" + ip +"/test.php");
    webBrowser1.DocumentText = System.Text.ASCIIEncoding.ASCII.GetString(response);
}
catch(Exception e)
{
    MessageBox.Show("Download failed");
}
Ali Vojdanian
  • 2,067
  • 2
  • 31
  • 47
  • I have tried this. This will only upload file to php server. But I need to send a string with the File. Let the string will be the name of folder so I can identify in which folder file should be saved. – MouseCrasher Oct 31 '12 at 12:25
  • @MouseCrasher in web.UploadFile("http://" + ip + "/test.php", StoreID); you can send the string. also you can upload that in .zip file so that you can upload the string file too. – Ali Vojdanian Oct 31 '12 at 12:27
  • you are right but in my case just look at the code `Client.UploadFile("http://localhost/upload.php", "POST", "C:\\aaaa.jpg"); Client.UploadString("http://localhost/upload.php", "POST", StoreID);` these are two requests and i want to call only one request to send _aaaa.jpg_ and _StoreID_ – MouseCrasher Oct 31 '12 at 12:32
  • Nope.. I want to send two items at a single request. First is aaaa.jpg and second is StoreID. You can understand this like, I just want to call Upload function only once... – MouseCrasher Oct 31 '12 at 12:52
  • @MouseCrasher http://stackoverflow.com/questions/2854052/upload-two-files-at-once it will show you how to do that – Ali Vojdanian Oct 31 '12 at 19:49
0

You can create a webservice with php that accepts a file. Then publish that webservice, and add it to you c# references, then just call teh method from within your c# code that accepts the file, and vualá!

How to create SOAP with php link

VicoMan
  • 289
  • 2
  • 13