Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » U++ Library support » U++ MT-multithreading and servers » HttpRequest File Upload
HttpRequest File Upload [message #39557] Fri, 29 March 2013 13:23 Go to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Hello,
i want to upload an file to Rapidshare with HttpRequest.
Is there an example how to create a POST Request? And an example how to upload a file?
I found this code, but i dont whether it works or not and how to add other post parameters (like username, password)
	String boundary = "BbX8c0";
	StringBuffer b;
	b.Cat("--" + boundary + "\r\n");
	b.Cat("Content-Disposition: form-data; name=\"file\"; filename=\"" + file + "\"\r\n\r\n");
	b.Cat(fi);
	b.Cat("\r\n--" + boundary + "--\r\n");


//EDIT: Okay, now i know what was wrong. Right code is

	HttpRequest GetServer("http://api.rapidshare.com/cgi-bin/rsapi.cgi?sub=nextuploadserver");
	ulserver = GetServer.Execute();
	if(!GetServer.IsSuccess()){
		PromptOK("Couldn find uploadserver");
		return;
	}


	HttpRequest req("http://rs" + ulserver + ".rapidshare.com/cgi-bin/rsapi.cgi");
	req.POST();
	req.Post("sub", "upload");
	req.Post("login", "NAME");
	req.Post("password", "PASSWORD");
	req.Post("filename", file);
	req.Post("filecontent", LoadFile(file));
	String s = req.Execute();

[Updated on: Fri, 29 March 2013 14:49]

Report message to a moderator

Re: HttpRequest File Upload [message #39566 is a reply to message #39557] Sat, 30 March 2013 11:50 Go to previous messageGo to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Okay,
seems like the code only works because the Rapidshare API allows to override the filename with the "filname" parameter.
When I wand to upload a file to share-online.biz, i cant pass the filename as an parameter.

void fUpload::Upload(){
	Vector <String> session; // to store SESSION and ServerURL
	int64 Filesize;
	String ret;
	FileIn fi;
	if(file == ""){
		PromptOK("No File selected");
		return;
	}

	HttpRequest GetServer("http://www.share-online.biz/upv3_session.php");
	GetServer.POST();
	GetServer.Post("username", "USER");
	GetServer.Post("password", "PW");
	ret = GetServer.Execute();
	if(!GetServer.IsSuccess()){
		PromptOK("Couldnt find uploadserver");
		return;
	}
	session = Split(ret, ";");
	if(!fi.Open(file)){
		PromptOK("Couldnt open " + file);
		return;
	}
	Filesize = fi.GetSize();
	fi.Close();

	HttpRequest	req(session[1]);
	req.POST();
	req.Post("username", "USER");
	req.Post("password", "PW");
	req.Post("upload_session", session[0]);
	req.Post("chunk_no", "1");
	req.Post("chunk_number", "1");
	req.Post("filesize", AsString(Filesize));
	req.Post("fn", LoadFile(file));
	req.Post("finalize", "1");
	ret = req.Execute();
	if(!req.IsSuccess()){
		PromptOK("FAILED");
		PromptOK(req.GetErrorDesc());
		return;
	}
	
	PromptOK(ret);
}

The second request doesnt work cause there is no filename parameter. How can i pass the filename without a filename parameter?
Re: HttpRequest File Upload [message #39568 is a reply to message #39566] Sat, 30 March 2013 12:13 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Brat wrote on Sat, 30 March 2013 06:50

Okay,
seems like the code only works because the Rapidshare API allows to override the filename with the "filname" parameter.
When I wand to upload a file to share-online.biz, i cant pass the filename as an parameter.

void fUpload::Upload(){
	Vector <String> session; // to store SESSION and ServerURL
	int64 Filesize;
	String ret;
	FileIn fi;
	if(file == ""){
		PromptOK("No File selected");
		return;
	}

	HttpRequest GetServer("http://www.share-online.biz/upv3_session.php");
	GetServer.POST();
	GetServer.Post("username", "USER");
	GetServer.Post("password", "PW");
	ret = GetServer.Execute();
	if(!GetServer.IsSuccess()){
		PromptOK("Couldnt find uploadserver");
		return;
	}
	session = Split(ret, ";");
	if(!fi.Open(file)){
		PromptOK("Couldnt open " + file);
		return;
	}
	Filesize = fi.GetSize();
	fi.Close();

	HttpRequest	req(session[1]);
	req.POST();
	req.Post("username", "USER");
	req.Post("password", "PW");
	req.Post("upload_session", session[0]);
	req.Post("chunk_no", "1");
	req.Post("chunk_number", "1");
	req.Post("filesize", AsString(Filesize));
	req.Post("fn", LoadFile(file));
	req.Post("finalize", "1");
	ret = req.Execute();
	if(!req.IsSuccess()){
		PromptOK("FAILED");
		PromptOK(req.GetErrorDesc());
		return;
	}
	
	PromptOK(ret);
}

The second request doesnt work cause there is no filename parameter. How can i pass the filename without a filename parameter?


I guess it this is really service specific, anyway I would dare to say that alternative to passing filename parameter would be "RESTfull" - pass it as part of url. Perhaps something like

HttpRequest req(session[1] + '/' + filename);

Anyway, once again, there is not firm standard...

Mirek
Re: HttpRequest File Upload [message #39570 is a reply to message #39557] Sat, 30 March 2013 12:44 Go to previous messageGo to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Hello Mirek,
i'll try it this way. But how does an Fileuplaod normaly work?
How does a browser handle a fileupload? I guess there is an other way.
For example this form

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



The "enctype="multipart/form-data"" causes a Multipart request.
http://www.ietf.org/rfc/rfc2388.txt

Is there a way to perfom those request with u++?
Re: HttpRequest File Upload [message #39571 is a reply to message #39570] Sat, 30 March 2013 12:51 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Brat wrote on Sat, 30 March 2013 07:44

Hello Mirek,
i'll try it this way. But how does an Fileuplaod normaly work?
How does a browser handle a fileupload? I guess there is an other way.
For example this form

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>



The "enctype="multipart/form-data"" causes a Multipart request.
http://www.ietf.org/rfc/rfc2388.txt

Is there a way to perfom those request with u++?



There is no "easy" support in HttpRequest now, but you can use "PostData" and create the whole content part..
Re: HttpRequest File Upload [message #39573 is a reply to message #39571] Sat, 30 March 2013 21:11 Go to previous messageGo to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
Okay,
i have to calculate the new Content-Length - how can i do this? strlen()? And how can i manipulate the Content-Length field?

#EDIT:
Okay, i created this class.
class MultiHTTP {
	public:
		MultiHTTP();
		MultiHTTP(String boundary);
		String GetContentType();
		String GetBoundary();
		void AddData(String name, String content);
		void AddFile(String name, String filename, String mime, String content);
		String GetRequest();
		
	private:
		String bound;
		String req;
		int64 Length;
};

MultiHTTP::MultiHTTP(){
	bound = "---MULTIPART-BOUNDARY---" + AsString(Random()) + "---" + AsString(Random()) + "---";
	req = bound + "\n";
}

MultiHTTP::MultiHTTP(String boundary){
	bound = boundary;
	req = bound + "\n";
}

String MultiHTTP::GetContentType(){
	return "multipart/form-data, boundary=" + bound;
}

String MultiHTTP::GetBoundary(){
	return bound;
}

void MultiHTTP::AddData(String name, String content){
	req += "Content-Disposition: form-data; name=\"" + name +"\"\n\n";
	req += content + "\n" + bound + "\n";
}

void MultiHTTP::AddFile(String name, String filename, String mime, String content){
	req += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\n";
	req += "Content-Type: " + mime + "\n";
	req += "Content-Transfer-Encoding: binary\n\n";
	req += content + "\n" + bound + "\n";
	
}

String MultiHTTP::GetRequest(){
	return req;
}


And my Code is

	MultiHTTP request;
	request.AddData("username", "USER");
	request.AddData("password", "PASSWORD");
	request.AddData("upload_session", session[0]);
	request.AddData("chunk_no", "1");
	request.AddData("chunk_number", "1");
	request.AddData("filesize", AsString(Filesize));
	request.AddData("finalize", "1");
	request.AddFile("fn", file, "text/plain", LoadFile(file));
	LOG(request.GetRequest());

	HttpRequest	req(session[1]);
	req.POST();
	req.ContentType(request.GetContentType());
	req.PostData(request.GetRequest());
	ret = req.Execute();
	if(!req.IsSuccess()){
		PromptOK("FAILED");
		PromptOK(req.GetErrorDesc());
		return;
	}
	LOG(ret);


My class created this POST data
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="username"

USERNAME
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="upload_session"

O1QQRHKMDIDI
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---657443953---2000568409---

But the Request fails. i only got this error
socket(6) / recv: Connection reset by peer


Whats wrong?

[Updated on: Sat, 30 March 2013 22:19]

Report message to a moderator

Re: HttpRequest File Upload [message #39574 is a reply to message #39573] Sun, 31 March 2013 11:48 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Brat wrote on Sat, 30 March 2013 16:11

Okay,
i have to calculate the new Content-Length - how can i do this? strlen()? And how can i manipulate the Content-Length field?

#EDIT:
Okay, i created this class.
class MultiHTTP {
	public:
		MultiHTTP();
		MultiHTTP(String boundary);
		String GetContentType();
		String GetBoundary();
		void AddData(String name, String content);
		void AddFile(String name, String filename, String mime, String content);
		String GetRequest();
		
	private:
		String bound;
		String req;
		int64 Length;
};

MultiHTTP::MultiHTTP(){
	bound = "---MULTIPART-BOUNDARY---" + AsString(Random()) + "---" + AsString(Random()) + "---";
	req = bound + "\n";
}

MultiHTTP::MultiHTTP(String boundary){
	bound = boundary;
	req = bound + "\n";
}

String MultiHTTP::GetContentType(){
	return "multipart/form-data, boundary=" + bound;
}

String MultiHTTP::GetBoundary(){
	return bound;
}

void MultiHTTP::AddData(String name, String content){
	req += "Content-Disposition: form-data; name=\"" + name +"\"\n\n";
	req += content + "\n" + bound + "\n";
}

void MultiHTTP::AddFile(String name, String filename, String mime, String content){
	req += "Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\"\n";
	req += "Content-Type: " + mime + "\n";
	req += "Content-Transfer-Encoding: binary\n\n";
	req += content + "\n" + bound + "\n";
	
}

String MultiHTTP::GetRequest(){
	return req;
}


And my Code is

	MultiHTTP request;
	request.AddData("username", "USER");
	request.AddData("password", "PASSWORD");
	request.AddData("upload_session", session[0]);
	request.AddData("chunk_no", "1");
	request.AddData("chunk_number", "1");
	request.AddData("filesize", AsString(Filesize));
	request.AddData("finalize", "1");
	request.AddFile("fn", file, "text/plain", LoadFile(file));
	LOG(request.GetRequest());

	HttpRequest	req(session[1]);
	req.POST();
	req.ContentType(request.GetContentType());
	req.PostData(request.GetRequest());
	ret = req.Execute();
	if(!req.IsSuccess()){
		PromptOK("FAILED");
		PromptOK(req.GetErrorDesc());
		return;
	}
	LOG(ret);


My class created this POST data
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="username"

USERNAME
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="upload_session"

O1QQRHKMDIDI
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---657443953---2000568409---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---657443953---2000568409---

But the Request fails. i only got this error
socket(6) / recv: Connection reset by peer


Whats wrong?


Hard to say. Try HttpRequest::SetTrace.

Mirek
Re: HttpRequest File Upload [message #39577 is a reply to message #39574] Sun, 31 March 2013 20:41 Go to previous messageGo to next message
Brat is currently offline  Brat
Messages: 7
Registered: March 2013
Promising Member
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---

---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"

USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"

V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---2965041788---1648970112---

HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---

---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"

USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"

V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---2965041788---1648970112---

HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---

---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"

USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"

V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---2965041788---1648970112---

HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
HTTP retry on error socket(6) / recv: Connection reset by peer
Starting status 1 'Start', url: dlw191-2.share-online.biz
HTTP Execute: Start
HTTP START
Starting status 2 'Resolving host name', url: dlw191-2.share-online.biz
HTTP StartConnect
HTTP AfterConnect
Starting status 6 'Sending request', url: dlw191-2.share-online.biz
HTTP REQUEST dlw191-2.share-online.biz:0
HTTP request:
POST /upload HTTP/1.1
URL: http://dlw191-2.share-online.biz/upload
Host: dlw191-2.share-online.biz
Connection: close
Accept: */*
Accept-Encoding: gzip
User-Agent: U++ HTTP request
Content-Length: 1059
Content-Type: multipart/form-data, boundary=---MULTIPART-BOUNDARY---2965041788---1648970112---

---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="username"

USER
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="password"

PASSWORD
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="upload_session"

V1URFJKMAR81UV
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_no"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="chunk_number"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="filesize"

69
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="finalize"

1
---MULTIPART-BOUNDARY---2965041788---1648970112---
Content-Disposition: form-data; name="fn"; filename="NeedIt.txt"
Content-Type: text/plain
Content-Transfer-Encoding: binary

Sonic Youth
Deftones
Death From Above 1979 

(Andrew Jackson Jihad)


---MULTIPART-BOUNDARY---2965041788---1648970112---

HTTP Execute: Sending request
Starting status 7 'Receiving header', url: dlw191-2.share-online.biz
HTTP Execute: Receiving header
Re: HttpRequest File Upload [message #39614 is a reply to message #39577] Sat, 06 April 2013 10:05 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
The critical line is

HTTP retry on error socket(6) / recv: Connection reset by peer


- means server disconnected you after recieving the request. Most likely there is something wrong with it.

Wild guess: Most servers (U++'s Skylark included) require some tokens in POST request to prevent

http://en.wikipedia.org/wiki/Cross-site_request_forgery

Means you have to imitate the browser requests a bit more that just sending plain POST.

Mirek
Re: HttpRequest File Upload [message #40184 is a reply to message #39614] Sat, 29 June 2013 20:53 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
HttpRequest now supports multipart requests directly with "Part" method...

Mirek
Re: HttpRequest File Upload [message #40961 is a reply to message #40184] Mon, 14 October 2013 15:51 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Does HttpRequest have support for multipart response from server as well?
Re: HttpRequest File Upload [message #40962 is a reply to message #40961] Mon, 14 October 2013 16:00 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Well, there is no problem in receiving multipart response, but you will have to parse the body yourself. (In fact, proper parsing routine is in Skylark, perhaps it would make sense to move it to Core).

Or do do you mean

http://en.wikipedia.org/wiki/Chunked_transfer_encoding

? That is supported. You can even use WhenOut and avoid storing output data into single String.
Re: HttpRequest File Upload [message #40963 is a reply to message #40962] Mon, 14 October 2013 16:47 Go to previous messageGo to next message
Zbych is currently offline  Zbych
Messages: 325
Registered: July 2009
Senior Member
Server response looks like this:

Content-Length: 111970
Content-MD5: c0c1946bc49372111b25c3aafe7327b1
Content-Type: multipart/related; boundary=blablabla
Date: Mon, 14 Oct 2013 13:52:06 GMT
Server: ServerName/1.0

Content-Type: application/json, charset=UTF-8
Content-ID: body

Some Json stuff here
--blablabla
Content-Type: application/octet-stream
Content-ID: attachment

Some binary data here
--blablabla--



Can you point me to some functions that I can extract from skylark, to parse this response? I guess that ReadMultiPart is the function that I am looking for.



[Updated on: Mon, 14 October 2013 16:52]

Report message to a moderator

Re: HttpRequest File Upload [message #40968 is a reply to message #40963] Tue, 15 October 2013 07:44 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Correct.

Mirek
Re: HttpRequest File Upload [message #43117 is a reply to message #40184] Thu, 08 May 2014 14:35 Go to previous messageGo to next message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
Hello,

I am resuming this old post.
I am having problem with the upload file.
This a working PHP file (tested by browser via a html file driver) located in /var/www/vega/logup
<?php
//transfer.php
function Upload($folder)
{       $target = "./". basename( $_FILES['file']['name']) ; 
        echo $target;
        if(copy($_FILES['file']['tmp_name'], $target)) return 1;
        else return 0;
}
Upload("");
?>

It saves the file in the same folder. This is the U++ driver program:
void PublishSiteDlg::PublishVega()
{ 	String fn = TD.PATHDIRWWW+"compress.zip";
	if (FileExists(fn)) DeleteFile(fn);
	FileZip zip(fn);
        String targetdir = TD.PATHDIRWWW;
	FindFile fff(AppendFileName(targetdir, "*.*"));
	while(fff) {
			if(fff.IsFile()) {
		          zip.WriteFile(LoadFile(TD.PATHDIRWWW+fff.GetName()), fff.GetName());
			}
			fff.Next();
	}
	zip.Finish();	//all OK here and the file is created

	HttpRequest req("http://localhost/vega/logup/transfer.php");
	req.POST();
	req.Part("file", LoadFile(fn), "application/zip", fn);
	req.Post("submit", "submit");
	String s = req.Execute();
	Exclamation(s);
}

The previous code shows the echoed file name but no file is uploaded.
What I am missing?
Thanks,
Luigi
Re: HttpRequest File Upload [message #43123 is a reply to message #43117] Sun, 11 May 2014 22:17 Go to previous message
forlano is currently offline  forlano
Messages: 1182
Registered: March 2006
Location: Italy
Senior Contributor
...little update.

I tried another experiment substituting req.Part(...) with

req.Post("file", LoadFile(fn));

to upload the zipped file. After a slight modification of the PHP script on the server, the file is uploaded and saved where I want. Unfortunately the server is not able to unzip it. It seems that during the transfer the file lost some encoding and the server is not able to understand how to unzip it Rolling Eyes . In fact when I put it back on my computer the unzip no longer work.

If nobody has experience with file upload via U++ I'll try http://curl.haxx.se/libcurl .

Thanks,
Luigi
Previous Topic: How to use Skylark on external server
Next Topic: [SOLVED] Fix some memory leak in IpAddrInfo
Goto Forum:
  


Current Time: Fri Mar 29 01:37:15 CET 2024

Total time taken to generate the page: 0.01163 seconds