1

I am using this code to upload a generic file stored in IsolatedStorage but it doesn't work:

string Filename = "aaa.dat";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://(mysite)/upload.php");
        request.Method = "POST";
        request.ContentType = "multipart/form-data";
        string postData = String.Format("user_file", Filename);   

        // Getting the request stream.
        request.BeginGetRequestStream
            (result =>
            {
                // Sending the request.
                using (var requestStream = request.EndGetRequestStream(result))
                {
                    using (StreamWriter writer = new StreamWriter(requestStream))
                    {
                        writer.Write(postData);
                        writer.Flush();
                    }
                }

                // Getting the response.
                request.BeginGetResponse(responseResult =>
                {
                    var webResponse = request.EndGetResponse(responseResult);
                    using (var responseStream = webResponse.GetResponseStream())
                    {
                        using (var streamReader = new StreamReader(responseStream))
                        {
                            string srresult = streamReader.ReadToEnd();
                        }
                    }
                }, null);
            }, null);

This is my php file:

<?php

define("UPLOAD_DIR", "./uploads/");

if(isset($_POST['action']) and $_POST['action'] == 'upload')
{
    if(isset($_FILES['user_file']))
    {
        $file = $_FILES['user_file'];
        if($file['error'] == UPLOAD_ERR_OK and is_uploaded_file($file['tmp_name']))
        {
            move_uploaded_file($file['tmp_name'], UPLOAD_DIR.$file['name']);
            echo "ok";
        }
    }
}

?>

Someone can tell me why this code doesn't work ?

dav_i
  • 27,509
  • 17
  • 104
  • 136
xRobot
  • 25,579
  • 69
  • 184
  • 304

1 Answers1

1

It does not work because the following line

string postData = String.Format("user_file", Filename);   

Is equivalent to

string postData = "user_file";

And the actual file data is never included into the request. String.Format is used to include variables into a pattern, ie:

string logMessage = String.Format("Uploading {0}", Filename);

Would generate "Uploading aaa.dat".

If you want to make this work you'll have to:

  • Read the contents of the file
  • Conform to the rules of multipart/form-data RFC1867
C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Why is it equivalent to that ? And how can I resolve ? Thanks – xRobot Jan 21 '14 at 15:07
  • @xRobot I don't know if it's that simple. .NET has alot of magic under the hood, but unless there is already something built in to generate multipart/form-data requests, you'll have to do alot more than change one line of code. – C.Evenhuis Jan 21 '14 at 15:16
  • See http://stackoverflow.com/questions/566462/upload-files-with-httpwebrequest-multipart-form-data for an example. – C.Evenhuis Jan 21 '14 at 15:18
  • Is there another way to upload a file ? – xRobot Jan 21 '14 at 15:19
  • @xRobot there definately is, but chances are php would not regard it as an uploaded file. See if the link I shared a second ago works for you. – C.Evenhuis Jan 21 '14 at 15:21
  • So, if not php, what other server side language is good in my case ? I am reading that link thanks... wow, all that code to simply upload a file :D – xRobot Jan 21 '14 at 15:23
  • Could I convert that file in a string ( base64 for example ) ? – xRobot Jan 21 '14 at 15:33
  • @xRobot Yes you could send data as base64. You could probably even access the data using http://www.php.net/manual/en/function.http-get-request-body.php. Use headers `Content-Type: application/octet-stream` and `Content-Transfer-Encoding: base64`. – C.Evenhuis Jan 21 '14 at 16:03
  • Ok, I just tryed, but doesn't work :( http://stackoverflow.com/questions/21265075/how-convert-a-file-to-base64-and-then-upload-it – xRobot Jan 21 '14 at 17:31
  • You did not use `http-get-request-body` in the php script. It should contain exactly what you sent. – C.Evenhuis Jan 21 '14 at 18:25
  • ehm, excuse me but what is http-get-request-body and where do I have to use it ? thanks :) – xRobot Jan 21 '14 at 18:30
  • You should use it instead of `$_POST['file']` in that example. It should contain the data exactly as you sent it. What you do with the data is up to you of course. – C.Evenhuis Jan 21 '14 at 18:57
  • So do I have to use $_GET['file'] instead of $_POST['file'] ? – xRobot Jan 21 '14 at 18:59
  • http://www.php.net/manual/en/function.http-get-request-body.php look I'm not a php specialist, and your question was "what's wrong with this code". Browse around, try a few things, debug, debug, debug, you'll learn more than just asking every step... – C.Evenhuis Jan 21 '14 at 20:16