KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I'm trying to upload a file and a send along a few parameters to my site using .NET / C#. Having read a few tutorials that do either a few parameters or a file, I've tried, unsuccessfully, to combine them. Here is how I try doing it: WebRequest req = WebRequest.Create(baseURL + "upload"); req.Credentials = new NetworkCredential(username, password); String boundary = "B0unD-Ary"; req.ContentType = "multipart/form-data; boundary=" + boundary; req.Method = "POST"; ((HttpWebRequest)req).UserAgent = "UploadTester v0.1"; string postData = "--" + boundary + "\nContent-Disposition: form-data\n"; postData += "myId=123&someFk=456"; postData += "\n--" + boundary + "\nContent-Disposition: form-data; name=\"file\" filename=\"upload.pdf\" Content-Type: application/pdf\n\n"; byte[] byteArray = Encoding.UTF8.GetBytes(postData); byte[] filedata = null; using (BinaryReader reader = new BinaryReader(File.OpenRead("myfile.pdf"))) filedata = reader.ReadBytes((int)reader.BaseStream.Length); req.ContentLength = byteArray.Length + filedata.Length; req.GetRequestStream().Write(byteArray, 0, byteArray.Length); req.GetRequestStream().Write(filedata, 0, filedata.Length); WebResponse response = req.GetResponse(); Stream data = response.GetResponseStream(); StreamReader sReader = new StreamReader(data); String sResponse = sReader.ReadToEnd(); response.Close(); When I execute it, I get a 500 exception, saying "Header section has more than 10240 bnytes (maybe it is not properly terminated)" and Wireshark informs me that the request sent was a malformed package, where the MIME multipart was malformed. There are probably several issues here, so please let me know all the problems you can spot Update : to separate MIME from C#/.NET, I've spawned a thread here: https://stackoverflow.com/questions/1880002/error-in-mime-packet-for-http-
Tags (comma-separated)
Save Edits
Cancel