Upload multiple files to one HTTPWebRequest

I created a service that takes 2 things:

1) The body parameter, called "type".

2) Downloadable csv file.

I read these two things on the server side as follows:

 //Read body params
 string type = HttpContext.Current.Request.Form["type"];

 //read uploaded csv file
 Stream csvStream = HttpContext.Current.Request.Files[0].InputStream;

How can I check this, I use Fiddler to check this, but I can only send one thing at a time (type or file), because both things have different types of content, how can I use the content type multipart / form-data Strong> and application / x-www-form-urlencoded at the same time.

Even i use this code

    public static void PostDataCSV()
    {
        //open the sample csv file
        byte[] fileToSend = File.ReadAllBytes(@"C:\SampleData.csv"); 

        string url = "http://localhost/upload.xml";
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.Method = "POST";
        request.ContentType = "multipart/form-data";
        request.ContentLength = fileToSend.Length;


        using (Stream requestStream = request.GetRequestStream())
        {
            // Send the file as body request. 
            requestStream.Write(fileToSend, 0, fileToSend.Length);
            requestStream.Close();
        }

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        //read the response
        string result;
        using (StreamReader reader = new StreamReader(response.GetResponseStream()))
        {
            result = reader.ReadToEnd();
        }

        Console.WriteLine(result);
    }

It is also not sending any files to the server.

+3
source share
2 answers

contentTypes : http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7.2

multipart/form-data - http.

0

All Articles