0
Answer

Uploading files with HttpWebRequest in C#

Ask a question
Marcus

Marcus

18y
3.2k
1

Hello.

Im trying to upload a file to a webserver which has the following HTML Form.

The parameters should be sent in the message as in POST and not in the URL.

<FORM ACTION="linkTOASP-Page";
ENCTYPE="MULTIPART/FORM-DATA" METHOD="POST">

<INPUT TYPE="file" NAME="file1">

<INPUT TYPE="Hidden" NAME="path">
<INPUT TYPE="Hidden" NAME="folder">

<INPUT TYPE="submit" VALUE="Submit">
</FORM>

I call my function like this

NameValueCollection paramters = new NameValueCollection();

parameters ["path"] = "1";

parameters ["folder"] = "1";

string file;

file = "C:\\test.txt”;

string answer = uploadFile(file, linkTOASP-Page , "file1", parameters);

This is my code for handling the upload...but it's not working :(

        public static string uploadFile(string filePath, string url,

            string nameOfFileParameter, NameValueCollection parameters)

        {

 

            string postdata;

            postdata = "?";

            if (parameters != null)

            {

 

                foreach (string key in parameters.Keys)

                {

                    postdata += key + "=" + parameters.Get(key) + "&";

                }

 

            }

 

            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[] data = encoding.GetBytes(postdata);

 

            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);

            webrequest.ContentType = "multipart/form-data;";

            webrequest.Method = "POST";

 

            string filedata = nameOfFileParameter + "=" + Path.GetFileName(filePath);

            byte[] filedataBytes = encoding.GetBytes(filedata);

 

            FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

 

            long length = data.Length + filedataBytes.Length + fileStream.Length;

            webrequest.ContentLength = length;

 

            Stream requestStream = webrequest.GetRequestStream();

 

           //write hidden parameters

            requestStream.Write(data, 0, data.Length);

 

//write filename

            requestStream.Write(filedataBytes, 0, filedataBytes.Length);

 

           //write file

            byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];

            int bytesRead = 0;

            while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)

                requestStream.Write(buffer, 0, bytesRead);

 

            requestStream.Flush();

            requestStream.Close();

 

            WebResponse responce = webrequest.GetResponse();

            Stream s = responce.GetResponseStream();

            StreamReader sr = new StreamReader(s);

 

            sr.Close();

            s.Close();

            fileStream.Close();

 

            return sr.ReadToEnd();

        }

 

Im getting "invalid path characters" as a server error in server log if that helps.

Thx for any help