0

I have been working on my first C# application lately and I finished working on the interface - it's perfect.

Now I am stuck at the part where I get an Open File dialog and then send that file to my PHP script located on localhost (wamp).

This is what I've been trying to do:

        private void menu_upload_file_Click(object sender, EventArgs e)
        {
            DialogResult dialogOpened = openFileDialog1.ShowDialog();
            if (dialogOpened == DialogResult.OK)
            {
                string filename = openFileDialog1.FileName;

                using (var client = new WebClient())
                {
                    client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload", "POST", filename);
                }
            }
        }

Nothing is happening and the file is not being sent (copied over) to my desired location.

You might understand that I am succeeding on opening the Open File dialog, but I have no clue if I am catching the filename/directory and send it over to my website so it can deal with it.

Maybe the url is complicated? I need to send those submit and action parameters or the script wont load.

OH AND: In PHP, the $_FILES array MUST BE named images ($_FILES['images']), maybe this is the problem? (thought of this while asking here).

So what is the problem that the file is not being sent to my website?

5
  • Do you have a try..catch somewhere that is swallowing the exception being raised when WebClient.UploadFile fails (assuming that is the issue)? Commented Feb 5, 2013 at 20:21
  • No, I do not, because I have no idea where to apply the try-catch and what Exception it might throw. Maybe you can help out here? Commented Feb 5, 2013 at 20:23
  • 1
    Have you tried using WireShark to see if the file gets transferred across the web? Have you looked at the logs on the "dugi" server? You said your WAMP is in localhost. Do you need to upload to LOCALHOST instead of http DUGI? Commented Feb 5, 2013 at 20:27
  • Have you tried stepping through the code in debug mode and following the code step by step to see where it fails? Commented Feb 5, 2013 at 20:29
  • @mbeckish I just did and I can't see where it's failing. I get the response below. Commented Feb 5, 2013 at 20:39

2 Answers 2

2

Assuming the client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload", "POST", filename); is not throwing an exception try to look at the response.

var response = client.UploadFile("http://dugi/imgitv3/upload.php?submit=true&action=upload",  "POST", filename);

Console.WriteLine("\nResponse Received.The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(response));
Sign up to request clarification or add additional context in comments.

4 Comments

no idea why, but this doesn't seem to open a command line window for me.
I tried with debugging (first time I used it) and I get this response + response {byte[935]} byte[]
You have to open the Output window (under the View menu) to see the result in a GUI application.
Are you going to help me out on this @hegearon?
1

The problem was in my PHP script. I was always looking for $_FILES['images'] there and nothing else. Then after some googles, I noticed that C# sends the file array named with file ($_FILES['file']).

I did the necessary changes in my PHP script and now everything is working.

Comments

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.