1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

HttpUtils2 - Simple image upload

Discussion in 'Android Questions' started by capu81, Oct 9, 2012.

  1. capu81

    capu81 New Member Licensed 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:

    Code:
    <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:
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Why do you use multipart/form-data format? Which error did you encounter?

    Sending a single file is very simple. Done in almost a single PHP line.
     
  3. capu81

    capu81 New Member Licensed User

    Have you an example?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

  5. capu81

    capu81 New Member Licensed User

    Hi, i've tried with PostFile but with no success :BangHead:

    Code:
    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:
    Code:
    <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:
     
  6. Erel

    Erel Administrator Staff Member Licensed User

  7. capu81

    capu81 New Member Licensed User

    i've tryed with your example but i receive only one file of 0 byte

    Code:
    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.
     
  8. Erel

    Erel Administrator Staff Member Licensed User

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice