HttpUtils2 - Simple image upload

capu81

New Member
Licensed User
Longtime User
Hi, i'm making a app that use camera image and send it to a php webservice. I've two files, a csv and a jpg.

I've read post and post, page and page of code but i don't undestand how to do this!

I've four variables:

- jpg directory
- jpg filename
- csv directory
- csv filename

In my web server i've create a simple php page for receve files:

B4X:
<html>
<?php
do {
  if (is_uploaded_file($_FILES['image']['tmp_name'])) {
    // Controllo che il file non superi i 18 KB
    if ($_FILES['image']['size'] > 18432) {
      $msg = "<p>Il file non deve superare i 18 KB!!</p>";
//      break;
    }
    // Ottengo le informazioni sull'immagine
    list($width, $height, $type, $attr) = getimagesize($_FILES['image']['tmp_name']);
    // Controllo che le dimensioni (in pixel) non superino 160x180
    if (($width > 160) || ($height > 180)) {
      $msg = "<p>Dimensioni non corrette!!</p>";
//      break;
    }
    // Controllo che il file sia in uno dei formati GIF, JPG o PNG
    if (($type!=1) && ($type!=2) && ($type!=3)) {
      $msg = "<p>Formato non corretto!!</p>";
//      break;
    }
    // Verifico che sul sul server non esista giÃ*n file con lo stesso nome
    // In alternativa potrei dare io un nome che sia funzione della data e dell'ora
    if (file_exists('uploads/'.$_FILES['image']['name'])) {
      $msg = "<p>File giÃ*sistente sul server. Rinominarlo e riprovare.</p>";
//      break;
    }
    // Sposto il file nella cartella da me desiderata
    if (!move_uploaded_file($_FILES['image']['tmp_name'], 'uploads/'.$_FILES['image']['name'])) {
      $msg = "<p>Errore nel caricamento dell'immagine!!</p>";
      $msg = "<p>Ho caricato il file!!</p>";
//      break;
    }
        }
} while (false);
echo $msg;
?>
</html>

for send i use enctype="multipart/form-data"

What is the simplest code to send this two files??:BangHead:
 

capu81

New Member
Licensed User
Longtime User
Hi, i've tried with PostFile but with no success :BangHead:

B4X:
Sub button_ok_Click
   Dim job1 As HttpJob
   Dim job2 As HttpJob
   Dim job1link As String
   Dim job1dir As String
   Dim job1file As String
   Dim job2link As String
   Dim job2dir As String
   Dim job2file As String
   
   job1.Initialize("Job1", Me)
   job2.Initialize("Job2", Me)

   variabili_globali.Descrizione = Descrizione_txt.Text
   funzioni.SaveCSVFile(variabili_globali.CSVDirectory,variabili_globali.ImageName & ".csv",True)
   
   'UPLOAD
   ToastMessageShow("Caricamento immagine sul server del comune", True)
   job1link = "http://.................../sign/seam/resource/rest/foto/upload"
   job1dir = variabili_globali.PicturesDirectory
   job1file = variabili_globali.ImageName
   job1.PostFile(job1link,job1dir,job1file)
   
   ToastMessageShow("Caricamento csv sul server del comune", True)
   'link http://.................../sign/seam/resource/rest/foto/upload
   'directory variabili_globali.CSVDirectory
   'Filename variabili_globali.ImageName & ".csv"
   job2link = "http://web.comune.calenzano.fi.it/sign/seam/resource/rest/foto/upload"
   job2dir = variabili_globali.CSVDirectory
   job2file = variabili_globali.ImageName & ".csv"
   job2.PostFile(job2link,job2dir,job2file)
   
   'Activity.LoadLayout("camera")
   Descrizione_txt.Visible = False
   button_ok.Visible = False
   Descrizione_lbl.Visible = False
   Panel_Gray.Visible = False
   

End Sub

I've added job done sub and i've test server page with this page and it work:
B4X:
<html xmlns="http://www.w3.org/1999/xhtml">
<head><script>var _gaq=_gaq||[];_gaq.push(['_setAccount','UA-1063309-8']);_gaq.push(['_trackPageview']);(function(){var ga=document.createElement('script');ga.type='text/javascript';ga.async=true;ga.src=('https:'==document.location.protocol?'https://ssl':'http://www')+'.google-analytics.com/ga.js';var s=document.getElementsByTagName('script')[0];s.parentNode.insertBefore(ga,s);})();</script></head><body>
<h1>JAX-RS Upload Form</h1>
<form action="/sign/seam/resource/rest/foto/upload" method="post" enctype="multipart/form-data">
<p>
Select a file : <input type="file" name="uploadedFile" size="50"/>
</p>
<input type="submit" value="Upload"/>
</form>
</body>
</html>

When i try to send file i've this error:
"Cannot consume content type"

:sign0163:
 
Upvote 0

capu81

New Member
Licensed User
Longtime User
i've tryed with your example but i receive only one file of 0 byte

B4X:
Dim files As List
    files.Initialize
    Dim fd As FileData
    fd.Initialize
    fd.Dir = variabili_globali.PicturesDirectory2
    fd.FileName = variabili_globali.ImageName
    fd.KeyName = "upfile2"
    fd.ContentType = "multipart/form-data"
    files.Add(fd)
    'Add second file
    Dim fd As FileData
    fd.Initialize
    fd.Dir = variabili_globali.CSVDirectory2
    fd.FileName = variabili_globali.ImageName & ".csv"
    fd.KeyName = "upfile"
    fd.ContentType = "multipart/form-data"
    files.Add(fd)
    'Add name / values pairs (parameters)
    'Dim NV As Map
    'NV.Initialize
    'NV.Put("note1", "abc")
    'NV.Put("note2", "def")
    Dim req As HttpRequest
    req = MultipartPost.CreatePostRequest("http://.............../sign/seam/resource/rest/foto/upload", Null, files)
    hc.Execute(req, 1)

PicturesDirectory2 = File.DirRootExternal & "/CaleEarth/Images/"
CSVDirectory2 = File.DirRootExternal & "/CaleEarth/csv/"

I've tryed with content type "multipart/form-data" and "application/octet-stream" but don't work

Imagename and csv name are ok.
 
Upvote 0
Top