2
votes

I am trying to upload an image from an app to a remote host using codenameone

MultipartRequest cr = new MultipartRequest();
String  filePath = Capture.capturePhoto(Display.getInstance().getDisplayWidth(), -1);
cr.setUrl(url);
cr.setPost(true);
String mime="image/jpeg";
cr.addData("img", filePath, mime);

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
cr.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueueAndWait(cr);

On the PHP page i'm getting a null on the name of the image. Please assist

2

2 Answers

4
votes

You need to set file name and change "img" to "file" on line 6:

MultipartRequest cr = new MultipartRequest();
String  filePath = Capture.capturePhoto(Display.getInstance().getDisplayWidth(), -1);
cr.setUrl(url);
cr.setPost(true);
String mime="image/jpeg";
cr.addData("file", filePath, mime);
cr.setFilename("file", "MyImage.jpg");//any unique name you want

InfiniteProgress prog = new InfiniteProgress();
Dialog dlg = prog.showInifiniteBlocking();
cr.setDisposeOnCompletion(dlg);
NetworkManager.getInstance().addToQueueAndWait(cr);

Then on php side, call the file name:

$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);

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"] == "image/png")) && ($_FILES["file"]["size"] < 5000000) && in_array($extension, $allowedExts)) {
    if ($_FILES["file"]["error"] > 0) {
        $named_array = array("Response" => array(array("Status" => "error")));
        echo json_encode($named_array);
    } else {
        move_uploaded_file($_FILES["file"]["tmp_name"], "FolderPath/" . $_FILES["file"]["name"]);

        $Path = $_FILES["file"]["name"];
        $named_array = array("Response" => array(array("Status" => "ok")));
        echo json_encode($named_array);
    }
} else {
    $named_array = array("Response" => array(array("Status" => "invalid")));
    echo json_encode($named_array);
}

On a side note, always run Capture inside a try and catch. An exception would be thrown if user cancel the image capture or something else happened.

0
votes

My Solution for images from Storage

String fileName = "";
MultipartRequest multipartRequest = new MultipartRequest();
InfiniteProgress progressIndicator = new InfiniteProgress();
Dialog dialog = progressIndicator.showInifiniteBlocking();
InputStream is = Storage.getInstance().createInputStream(fileName);
multipartRequest.setUrl(SERVICEURL + "upload");
multipartRequest.setPost(true);
multipartRequest.setFailSilently(true);
multipartRequest.setTimeout(30000);
multipartRequest.addData("file", is, Storage.getInstance().entrySize(fileName), "image/jpg");
multipartRequest.setFilename("file", fileName);
NetworkManager.getInstance().addToQueueAndWait(multipartRequest);
multipartRequest.setDisposeOnCompletion(dialog);