Ali Jibran
Ali Jibran

Reputation: 368

Save Multipart Image File Sent From Android to Laravel 5.1

UPDATE The problem is solved. It was folder permission error. simple chmod corrected it.

The image gets sent easily. problem is , i am not able to save it to disk. I've tried several methods, but none working for me.

Here is Android Code

      mFileInputStream = new FileInputStream(
                Environment.getExternalStorageDirectory().toString()+"/Pictures/msp/" + mIFileName);
        URL mUrl = new URL(mPostURL);
        HttpURLConnection mHttpUrlConnection = (HttpURLConnection) mUrl.openConnection();
        mHttpUrlConnection.setDoInput(true);
        mHttpUrlConnection.setDoOutput(true);
        mHttpUrlConnection.setUseCaches(false);
        mHttpUrlConnection.setConnectTimeout(10000);
        mHttpUrlConnection.setChunkedStreamingMode(1024);
        mHttpUrlConnection.setInstanceFollowRedirects(false);
        mHttpUrlConnection.setRequestMethod("POST");
        mHttpUrlConnection.setRequestProperty("Connection", "Keep-Alive");
        mHttpUrlConnection.setRequestProperty("Content-Type",
                "multipart/form-data;boundary="+boundary);
        mHttpUrlConnection.setRequestProperty("charset", "utf-8");
        //Write Post Data
        DataOutputStream wr = new DataOutputStream(mHttpUrlConnection.getOutputStream());
        wr.writeBytes(mTwoHyphens + boundary + mLineEnd);
        wr.writeBytes("Content-Disposition: form-data; name=\"title\""+ mLineEnd);
        wr.writeBytes(mLineEnd);
        wr.writeBytes(mIFileName);
        wr.writeBytes(mLineEnd);
        wr.writeBytes(mTwoHyphens + boundary + mLineEnd);
        wr.writeBytes("Content-Disposition: form-data; name=\"description\""+ mLineEnd);
        wr.writeBytes(mLineEnd);
        wr.writeBytes("MSP Staff-Tailor-Merch Picture");
        wr.writeBytes(mLineEnd);
        wr.writeBytes(mTwoHyphens + boundary + mLineEnd);
        wr.writeBytes("Content-Disposition: form-data; name=\"photo\";filename=\""
                + mIFileName +"\"" + mLineEnd + "Content-Type: image/jpeg\r\n" +"\r\n");
        wr.writeBytes(mLineEnd);

Will someone kindly inform me how to read this uploaded image file in laravel 5.1 and save it to disk. (It's not base64 encoded). Request::hasfile('image') returns True. But any operation on file results in error.

Here is the laravel compact() output

{"reply":"success","title":"STAFF_faizan.png","description":"MSP Staff-Tailor-Merch Picture","photo":null,"filename":null}

And This one is laravel var_dump() out of all the Inputs.

["title"]=>  string(16) "STAFF_faizan.png"  ["description"]=>  string(30) "MSP Staff-Tailor-Merch Picture"  ["photo"]=>  object(Symfony\Component\HttpFoundation\File\UploadedFile)#29 (7) {    ["test":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>    bool(false)    ["originalName":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>    string(16) "STAFF_faizan.png"    ["mimeType":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>    string(10) "image/jpeg"    ["size":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>    int(25617)    ["error":"Symfony\Component\HttpFoundation\File\UploadedFile":private]=>    int(0)    ["pathName":"SplFileInfo":private]=>    string(14) "/tmp/phpG3oqEy"    ["fileName":"SplFileInfo":private]=>    string(9) "phpG3oqEy"  }}

Upvotes: 2

Views: 3015

Answers (1)

astroanu
astroanu

Reputation: 3971

try this.

$photo = Request::file('photo'); $photo->move('/path/to/move', 'filename' . '.' . $photo->getExtention());

Upvotes: 2

Related Questions