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!!!!

1

2 Answers 2

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");
}
Sign up to request clarification or add additional context in comments.

5 Comments

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 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.
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
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 stackoverflow.com/questions/2854052/upload-two-files-at-once it will show you how to do that
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

4 Comments

this is the alternate solution. But could it be possible without web service.?
not sure why you don't want to use a WS it would help you a lot
When your browser submits a form, it doesn't use a webservice. What @MouseCrasher is trying to do is reproduce this.
Why don't you append your string at the end of the file and then you subtract it on the server side, it's not very elegant but could work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.