Android Question Where and how to upload a .zip file?

U

unba1300

Guest
Hi. I'd like to test uploading a .zip file somewhere. Is it as simple as the following code using HttpUtils2?
B4X:
    'Send a POST request
    job2.Initialize("Job2", Me)
    job2.PostFile("http://www.b4x.com/print.php", File.DirInternal, "testfile.zip")
I get an UnknownHostException error when trying this. Do you know if I could do this with a free account at http://www.000webhost.com/
Thanks.
 
U

unba1300

Guest
Thanks for answering, NJDude. I created an account there and uploaded some .zip files using their file manager and was able to download and unzip them with my app using HttpUtils2 and Archiver.
But the PHP? scripts thing is completely new to me. Would that be required with all file sharing/hosting websites? Or would there be an easier website to use than the one I've chosen? Thanks again.
 
Upvote 0
U

unba1300

Guest
Been working on this all night, but I haven't got it quite right.

I created files index.html and upload_file.php and have them on my website and uploading works fine on my PC, but not with my app; I suspect because I am trying to use only the PHP script and lacking something from the HTML file.

Here is the index.html file:
HTML:
<html>
<body>

<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>

</body>
</html>

Here is the upload_file.php file:
PHP:
<?php

$allowedExts = array("gif", "jpeg", "jpg", "png", "txt", "zip");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "multipart/zip")
|| ($_FILES["file"]["type"] == "application/zip")
|| ($_FILES["file"]["type"] == "text/zip")
|| ($_FILES["file"]["type"] == "application/x-zip-compressed")
|| ($_FILES["file"]["type"] == "multipart/x-zip")
|| ($_FILES["file"]["type"] == "application/x-compressed")
|| ($_FILES["file"]["type"] == "application/octet-stream")
|| ($_FILES["file"]["type"] == "zip/zip")
|| ($_FILES["file"]["type"] == "zip")
|| ($_FILES["file"]["type"] == "application/x-stuffit")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 200000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br>";
    echo "Type: " . $_FILES["file"]["type"] . "<br>";
    echo "Size: " . ($_FILES["file"]["size"] / 200000) . " kB<br>";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }


?>

And this is my code in the HttpUtils2:
B4X:
'Activity module
Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
    Dim job2 As HttpJob
  
    'Send a POST request
    job2.Initialize("Job2", Me)
'    job2.PostFile("http://www.mywebsite.com/upload_file.php?file=zippedfiles.zip", File.DirInternal, "zippedfiles.zip") 'didn't work.
    job2.PostFile("http://www.mywebsite.com/upload_file.php", File.DirInternal, "zippedfiles.zip") 'didn't work.
      
End Sub

Sub JobDone (Job As HttpJob)
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        'print the result to the logs
        Log(Job.GetString)
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Results:
JobName = Job2, Success = true
Invalid file
<!-- Hosting24 Analytics Code -->
<script type="text/javascript" src="http://stats.hosting24.com/count.php"></script>
<!-- End Of Analytics Code -->

Any help would be greatly appreciated. Thanks.
 
Last edited by a moderator:
Upvote 0
U

unba1300

Guest
However,
PHP:
echo 'Type: ' . $_FILES['userfile']['type'];
does not return any value. Is there a way to check the file type being uploaded on the server side with this multipart protocol?
 
Upvote 0
U

unba1300

Guest
I have it working now with
PHP:
$extension = end(explode(".", $_FILES["userfile"]["name"]));
{
echo 'File type is ' . $extension . '. ';
}
It's just one of many checks I may do, but thanks for the warning. I guess a better approach is to somehow look inside the file for some expected content. Still learning.
 
Upvote 0
U

unba1300

Guest
Hi. I am now able to upload files using the MultipartPost module with the HTTP library, and download files using the HttpUtils2Service and HttpJob modules with the HttpUtils2 library, but when I put both into the same project it says I should remove one of the modules. Is there some other way I should handle the downloads now too? Thanks.
 
Upvote 0
U

unba1300

Guest
I think I have the download of a text file working now with HTTP, but how to access the file in the TempFolder? This is what I have so far...
B4X:
Sub Process_Globals
   Dim FileUrl As String
   FileUrl = "http://www.ccoxxx.sitexx.com/testing.txt"
End Sub
Sub Activity_Create(FirstTime As Boolean)
   HttpUtils.CallbackActivity = "aDownload"
   HttpUtils.CallbackJobDoneSub = "JobDone"
   HttpUtils.Download("GET Job1", FileUrl)
End Sub
Sub JobDone (Job As String)
   If HttpUtils.IsSuccess(FileUrl) Then
      Dim b As String
      b = HttpUtils.GetInputStream(FileUrl)
      Log(b) 'Shows (BufferedInputStream) java.io.BufferedInputStream@43ed3b60
   End If
End Sub
 
Upvote 0
U

unba1300

Guest
It was determined that I needed to use the MultipartPost module to do the uploads and I have that working and I believe that requires the HTTP library. I have also been able to do downloads with the HttpUtils2 library and tutorial. But I still can not get both to work within the same app. If I have both libraries checked, and HttpJob and HttpUtils2Service modules added, then I get an error stating that "HttpUtils2Service is declared twice. You should either remove the library reference or the code module." But if I remove those modules, and keep both libraries checked, then I get a compilation error stating "Unknown member: JobName" for the downloads. And if I uncheck the HTTP library, then the uploads won't work. All I wanted to do was upload and download zip files to a server from my app. I have read the entire Beginner's Guide, User's Guide, the new book, tutorials, and numerous posts. Could someone please help me accomplish this, and I will send a donation your way. Thanks.
 
Upvote 0
U

unba1300

Guest
Thanks Erel. I striped the app down to minimal and not getting the same errors, but the downloading part is wrong. Upload works okay. File is attached.
 
Upvote 0
U

unba1300

Guest
The error was "java.lang.Exception: Sub jobdone signature does not match expected signature." I changed
B4X:
Sub JobDone (Job As String)
to
B4X:
Sub JobDone (Job As HttpJob)
and it works now. Thanks.
 
Upvote 0
Top