Android Question How to upload a zip file?

gvoulg

Member
Licensed User
Longtime User
This code works and uploads a zip file to a server.
It runs on Linux
curl -F zipFile=@filename.zip -u userxx : passwxx "server url”
How can I send the file using B4A and HTTP PostMultipart?
this code fails (Bad Request etc)
B4X:
Dim fd As MultipartFileData
    Dim fname As String= P_ID & ".zip"
    Dim dirname As String= File.Combine( Starter.ArchiveFolder,P_ID)
    fd.Initialize
    fd.Dir = dirname
    fd.FileName =fname
    fd.KeyName = "zipFile"
    Dim j As HttpJob
    j.Initialize("j", Me)
    j.Username="userxx"
    j.Password="passwxx"
  j.PostMultipart( "server url",Null ,Array(fd))
any help appreciated
George
 

MarkusR

Well-Known Member
Licensed User
Longtime User
̶f̶̶d̶̶.̶̶d̶̶i̶̶r̶̶ ̶=̶ ̶̶l̶̶o̶̶o̶̶k̶̶s̶̶ ̶̶o̶̶d̶̶d̶̶ ̶̶y̶̶o̶̶u̶̶ ̶̶p̶̶u̶̶t̶̶ ̶̶p̶̶a̶̶t̶̶h̶+̶f̶̶i̶̶l̶̶e̶̶ ̶̶i̶̶n̶̶t̶̶o̶̶ ̶(̶.̶̶c̶̶o̶̶m̶̶b̶̶i̶̶n̶̶e̶)
and fd.ContentType is missing
 
Last edited:
Upvote 0

MarkusR

Well-Known Member
Licensed User
Longtime User
my mistake, false looked.
i would try to log the network traffic and compare it.
for my testings i often used a simple app as tpc listener as target ip : port

example what you sending from B4A (Lib OkHttpUtils2 2.61)
(The HTTP Header ends with 2x cr lf then the data comes with content-length: )
POST / HTTP/1.1
Content-Encoding: UTF8
Content-Type: multipart/form-data; boundary=---------------------------1461124740692
Content-Length: 296
Host: 192.168.178.50
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.5.0

-----------------------------1461124740692
Content-Disposition: form-data; name="zipFile"; filename="Hello.zip"
Content-Type:

PK ...

My Test Code without https
B4X:
Sub Test
      
    Dim fd As MultipartFileData
    Dim fname As String= "Hello.zip"
    fd.Initialize
    fd.Dir = File.DirAssets
    fd.FileName =fname
    fd.KeyName = "zipFile"
    Dim j As HttpJob
    j.Initialize("Job1", Me)
    j.Username="userxx"
    j.Password="passwxx"
      
    j.PostMultipart( "http://192.168.178.50",Null ,Array(fd))
  
End Sub

Public Sub JobDone(Job As HttpJob)
  
    Log("JobName = " & Job.JobName & ", Success = " & Job.Success)
    If Job.Success = True Then
        Select Job.JobName
            Case "Job1", "Job2"
                'print the result to the logs
                Log(Job.GetString)
            Case "Job3"
                'show the downloaded image
                Activity.SetBackgroundImage(Job.GetBitmap)
        End Select
    Else
        Log("Error: " & Job.ErrorMessage)
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
  
End Sub
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
It is actually a subfolder with the same name as the zip file name
To upload a zip with okhttp you need to create the .zip first. Okhttputils does not create a .zip for you.

1. Create the .zip using archiever lib
2. Upload this .zip then
 
Upvote 0

gvoulg

Member
Licensed User
Longtime User
To upload a zip with okhttp you need to create the .zip first. Okhttputils does not create a .zip for you.

1. Create the .zip using archiever lib
2. Upload this .zip then

This is the way I am doing it . I did not paste all my code.
My problem is the syntax of the Post multipart process.It does not seem to fit with server's requirements.
 
Upvote 0

gvoulg

Member
Licensed User
Longtime User
This is the code in JScript from a web page that successfully uploads a zip file on this server
B4X:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>DMTServerWeb :: Endpoint Test</title>

<script src="js/jquery-1.12.4.min.js"></script>
<script src="js/json-view/jquery.json-view.min.js" type="text/javascript"></script>

<link href="js/json-view/jquery.json-view.min.css" rel="stylesheet"/>

</head>
<body>

<h1>DMTServerWeb :: Endpoint Test</h1>


<h2>/upload/zip endpoint</h2>
<form id="form">
<table>
<tr style="vertical-align:top;"><th>method</th><th>path</th><th>ZIP File</th></tr>
<tr style="vertical-align:top;"><td>POST</td><td>/survey/upload</td>
<td>
<input type="file" name="zipFile" size="45" accept=".zip" /><br/>
</td>
</tr>
</table>
</form>

<button type="button" id="testit_button" onclick="javascript:testit();">Test it!</button>

<div id="resultDiv" style="border: 1px solid #AAA; background-color: #F5F5F5; padding: 20px; margin-top: 20px;"></div>

<script type="text/javascript">
function testit ()
{
$('#resultDiv').html('Waiting response...');
$('#test_it').prop( "disabled", true );
var fd = new FormData(document.getElementById("form"));
$.ajax({
url: "rest/upload/zip"
, type: "POST"
, contentType: false
, processData: false
, data : fd
, headers: {"TOKEN": "xxxx"}
, cache: false
}).then(function(data, status, jqxhr) {
$('#resultDiv').html("");
$('#resultDiv').jsonView(JSON.stringify(data, undefined, 4));

//console.log(jqxhr);
$('#test_it').prop( "disabled", false );
});
}
</script>

</body>
</html>
The job is done in testit function

how is this translated in multipart process in B4A?
George
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Here it is an incomplete list of content/type values.
 
Upvote 0
Top