Click here to Skip to main content
15,891,864 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys. I've a problem with the upload of pdf file on the server.I use HttpWebRequest and POST method of HTTP
This is my code:

string[] open = this.openFileDialog1.FileName.Split('\\');
           string nome = open[open.Length - 1];
           string[] temp = nome.Split('.');
           string extension = temp[1];
           HttpWebRequest req = (HttpWebRequest)WebRequest.Create(this.indirizzo + "servletUpload/controllerFiles");
           string post_data = this.openFileDialog1.FileName;
           string boundary = "AaB03x";
           string useragent = "_Webclient_Meetecho 1.0";
           req.UserAgent = useragent;

           req.ContentType = "multipart/form-data; boundary=" + boundary;
           req.Method = "POST";
           byte[] contents = FileToByteArray(post_data);
           MemoryStream postData = new MemoryStream();
           string newLine = "\r\n";
           StreamWriter sw = new StreamWriter(postData);
           sw.Write("--" + boundary + newLine);
           sw.Write("Content-Disposition: form-data; name=\"myFile\"" +"; filename=" + "\"" +  this.stanza + "-" + this.room + "."+extension + "\"" + newLine,"upload",this.openFileDialog1.FileName, newLine);
           sw.Write("Content-Type: application/octet-stream; charset=ISO-8859-1" + newLine);
           sw.Write("Content-Transfer-Encoding: binary" + newLine + newLine);
           sw.Write("VALUE" + newLine);
           sw.Write("--{0}--{1}", boundary, newLine);

           sw.Flush();


           postData.Write(contents, 0, contents.Length);
           sw.Write(newLine);


           sw.Flush();

           req.ContentLength = postData.Length;
           using (Stream s = req.GetRequestStream())

               postData.WriteTo(s);

          postData.Close();


FileToByteArray convert the file in a byte array.
I receive an internal server error

Where i fail???
Posted

1 solution

Are you able to put a break point (or otherwise validate that the post is making it to your server-side code)? If not, then maybe the file you're posting is larger than the maxRequestLength for your web server?

** EDIT ** just noticed you're targeting a java server - so ignore maxRequestLength attribute, that's .NET. (Could be something similar though) You'll need to find out what the error is on the server-side before you can get any useful replies here.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900