Is it possible use HttpUtils2 to upload file?

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can add it as a GET parameter:
B4X:
job2.PostFile("http://ccotesting.site11.com/serTestUpload.php?FileName=file.zip", File.DirInternal, varZipFileUp)


You will need to use StringUtils.EncodeUrl to encode the file name (if it has spaces or other non-ASCII characters).
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I think in the past I dealt with this by sending the fileName and the fileContent splitted by a "//" separator, then exploded the content in an array to set the file name.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You can add it as a GET parameter:
B4X:
job2.PostFile("http://ccotesting.site11.com/serTestUpload.php?FileName=file.zip", File.DirInternal, varZipFileUp)


You will need to use StringUtils.EncodeUrl to encode the file name (if it has spaces or other non-ASCII characters).

This was a choice I used, but then decided to turn it off. I am always afraid of GET variables, in such operations :)
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
The POST payload is not more secure than the GET parameters. Especially when both are sent from an app and not seen in the browser.
There is a tiny difference in my opinion. If one has the link, she can experiment with the GET parameter. But anyway, I am not that concerned with the security problem. I am more concerned with accidental bad inputs with gets when a user uses a link outside of an app. This happened every day in a intranet app of mine :)
 
Upvote 0
U

unba1300

Guest
What is the syntax when passing more than one variable using this approach? When I try
B4X:
job2.PostFile("http://ccotesting.site11.com/serTestUpload.php?FileName="&varZipFileUp&"Age="&i&"theSize="&varZipFileUpSize, File.DirInternal, varZipFileUp)
it assigns all three variables to FileName. Thanks.
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
You should add & inside the quotes as well.
B4X:
"your.php?var1=" & yourVar1 & "&var2=" & yourVar2
and so on...
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
@unum4848

PHP:
 $FileName = $_GET['FileName'];

is not equal to

PHP:
 $PostData = file_get_contents("php://input");

To write the file to your upload directory you have to write the contents of $PostData to a file.

PHP:
$PostData = file_get_contents("php://input");
file_put_contents('/path/to/uploaddir/upload.zip', $PostData);

I didn´t worked with php://input in all the years... So i did not really know what $PostData do have as Content.
php-doku says it uses less memory than using $HTTP_RAW_POST_DATA.

I suggest debugging the contents of $_POST after uploading and getting the $PostData. Write a log and have a look at this log after uploading a file...
Remember that the log should be writte to a path where the script can write (chmod 777 or 644)

PHP:
$file_handle = fopen('./b4a_'.date("Y-W", time()).'.log', 'a+');
fwrite($file_handle, "======== POST-Data (RAW)====================="."\r\n");
fwrite($file_handle, "PostData = ".$PostData."\r\n");
fwrite($file_handle, "======== POST-Data ========================="."\r\n");
foreach($_POST as $name => $value){
  fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$name."=".$value." ".$add."\r\n");
}
fwrite($file_handle, "======== GET-Data ========================="."\r\n");
foreach($_GET as $name => $value){
  fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$name."=".$value." ".$add."\r\n");
}
fwrite($file_handle, "======================================"."\r\n");
fclose($file_handle);
 
Upvote 0

rossati

Active Member
Licensed User
Longtime User
Hello

I apologize but I am not able to upload a file, i.e. the JobDone event is not fired.
Where is my fauult?
hera the (simple code) whit some try, insertString and Download2, which works:
B4X:
    sendImg.Initialize("sendImgJob", Me)  
    sendImg.PostFile("http://rete.condorinformatique.com/upload.php?fileName=image.jpg",dir, fileName)
'    sendImg.PostString("http://rete.condorinformatique.com/upload.php", "&key2=value2&fileName=image.jpg")
'    sendImg.Download2("http://rete.condorinformatique.com/upload.php", _
'      Array As String("first key", "first value :)", "second key", "value 2"))
Sub JobDone (Job As HttpJob)
  ToastMessageShow("Done ", True)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  Job.Release
End Sub

best regards

John Rossati
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I've just tried this code and JobDone is raised:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Dim sendimg As HttpJob
   sendimg.Initialize("sendImgJob", Me)  
  sendimg.PostFile("http://rete.condorinformatique.com/upload.php?fileName=image.jpg", File.DirRootExternal, "1.xls")
End Sub
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  Job.Release
End Sub
 
Upvote 0

rossati

Active Member
Licensed User
Longtime User
Thanks
I have tried your script and my be one times worked, this get a strange answer:
B4X:
Sub Activity_Create(FirstTime As Boolean)
  Dim sendimg As HttpJob
  sendimg.Initialize("sendImgJob", Me) 
  sendimg.PostFile("http://rete.condorinformatique.com/upload.php?fileName=xximage.jpg", File.DirRootExternal, "image.jpg")
  sendimg.PostString("http://rete.condorinformatique.com/upload.php", "&key3=value2&fileName=image.jpg")
End Sub
Sub JobDone (Job As HttpJob)
  Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
  Log(Job.GetString)
  Job.Release
End Sub
The answer is:
B4X:
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
** Service (httputils2service) Start **
JobName = sendImgJob, Success = true
2047056561<pre>Array
(
)
Array
(
  [key3] => value2
  [fileName] => image.jpg
)
JobName = sendImgJob, Success = true

2132198191<pre>Array
(
)
Array
(
  [key3] => value2
  [fileName] => image.jpg
)
** Activity (main) Pause, UserClosed = false **
It seems two different instance of sendstring, here the PHP:
B4X:
<?PHP
echo rand()."<pre>";print_r($_FILES);
print_r($_REQUEST);
if (isset($_FILES['fileUpload']['name'])) {
    $flName = $_FILES['fileUpload']['name'];
    move_uploaded_file($_FILES['fileUpload']['tmp_name'],$flName );
}
?>

Timing problems? Android Samsung Version?

best regards

John Rossati
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
you should just use one of thesenot both
B4X:
 sendimg.PostFile("http://rete.condorinformatique.com/upload.php?fileName=xximage.jpg", File.DirRootExternal, "image.jpg")
  sendimg.PostString("http://rete.condorinformatique.com/upload.php", "&key3=value2&fileName=image.jpg")
 
Upvote 0

rossati

Active Member
Licensed User
Longtime User
thanks Manfred

this is a try, I have no answer if I use sendimg.PostFile and also if I invert the two instructions

regards John
 
Upvote 0

rossati

Active Member
Licensed User
Longtime User
Thanks Manfred

I apologize the time I have stealed to all, the probleme seem to be on my provider, I am asking confirm.
best regards

John
 
Upvote 0

EvgenyB4A

Active Member
Licensed User
Longtime User
Hi
I have the problem with post file that should be uploaded to ASP.NET page:

Dim job1 As HttpJob

job1.Initialize(
"Job1", Me)

'job1.PostFile(
"http://192.168.10.202/UniVend-Terminal/upload.aspx",File.DirRootExternal,"Cash00000027.txt")

The server gets the request but do not see any files.
At JobDone sub I get success.
When I send files from Windows PC application to the same page, the server sees the files.

What is wrong?
 
Upvote 0
Top