Hi Venky,
unfortunately i must admit, that i did found a solution for uploading a file neather with HttpConnection nor with Socket.
I tried the following:
1. Setup a servlet for uploading, build a small HTTP form and inspect the HTTP-Header:
Form:
<form enctype="multipart/form-data" action="FileUploader" method="POST">
File: <input name="uploadFile" type="file">
<input type="submit" value="upload">
</form>
HTTP-Header(truncated, only first bytes displayed):
POST /FileUploadServlet/FileUploader HTTP/1.1
Host: localhost:7070
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5 (.NET CLR 3.5.30729)
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://localhost:7070/FileUploadServlet/index.jsp
Cookie: JSESSIONID=0359fed1cc75cb347491ac227deb; _csuid=49057dd60a38b213
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 37953
-----------------------------41184676334
Content-Disposition: form-data; name="uploadFile"; filename="mytestpicture.jpg"
Content-Type: image/jpeg
ÿØÿ ...
This upload works fine, the servlet get the right information to save the uploaded file.
2. Try to upload a file with HttpConnection
var http = new HttpConnection("http://localhost:7070/FileUploadServlet/FileUploader" ) ;
var fileToTransfer = new File("mytestpicture.jpg") ;
http.requestheaders = ["Keep-Alive" , "300"] ;
http.method = "POST";
http.mime = "multipart/form-data;";
http.chunked = false;
http.request = fileToTransfer;
http.execute();
This doesn't work. The HttpConnection doesn't write any boundary or Content-Disposition information, so the servlet can't find the file content.
3. Try to upload afile with Socket
var conn = new Socket();
conn.open("localhost:7070");
conn.timeout=300;
conn.writeln("POST /FileUploadServlet/FileUploader HTTP/1.1");
conn.writeln("Host: localhost:7070");
conn.writeln("Keep-Alive: 300");
conn.writeln("Connection: keep-alive");
conn.writeln("Referer: http://localhost:7070/FileUploadServlet/");
conn.writeln("Content-Type: multipart/form-data; boundary=---------------------------1255014013694");
conn.writeln("Content-Length: 211");
conn.writeln("");
conn.writeln("-----------------------------1255014013694");
conn.writeln('Content-Disposition: form-data; name="uploadFile"; filename="test.txt"');
conn.writeln("Content-Type: text/plain");
conn.writeln("");
conn.writeln("test");
conn.write("-----------------------------1255014013694--");
var i=1;
while(!conn.eof) {
$.writeln(i + ":" + conn.eof + ":" + conn.readln());
++i;
};
conn.close();
This doesn't work. I tried to define an own boundary and set the Content-Disposition etc. by myself. For testing i used in this case no binary but a text file.
So Venky, perhaps i inspired you on further examination. Or perhaps Robin Mills got an idea on this issue (http://www.clanmills.com/robin.shtml) 😉 or any forum guru?!
Cheers Tino