【问题标题】:Uploading an image from Android (with Android Asynchronous Http Client) to laravel server将图像从 Android(使用 Android 异步 Http 客户端)上传到 laravel 服务器
【发布时间】:2015-06-09 22:25:48
【问题描述】:

我正在尝试通过 http post 方法将图像从 android 设备上传到 laravel 服务器。但是发布图片不起作用,发布参数(包括图片文件)似乎没有正确发送。

我正在使用 Android 异步 Http 客户端 (http://loopj.com/android-async-http/) 从 android 发布图像。这是代码:

安卓:

RequestParams params = new RequestParams();
    params.put("id_personil", session.getUID());
    params.put("id_deskel", session.getDID());
    params.put("jenis", jenisLap.getSelectedItemPosition());
    params.put("judul", judul.getText().toString());
    params.put("lokasi", lokasi.getText().toString());
    params.put("uraian", uraian.getText().toString());
    try{
        for (int a =0; a<imageUrl.size();a++){
            params.put("pic[]", new File(Environment.getExternalStorageDirectory().getPath()+imageUrl.get(0)));
        }
    }catch(FileNotFoundException e){e.printStackTrace();}

    AsyncHttpClient client = new AsyncHttpClient();
    client.post("http://10.0.3.2:8888/api/v1/lapgiat", params, new JsonHttpResponseHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
            super.onSuccess(statusCode, headers, response);
            ShowAlert(response.toString(), "text");
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
            super.onFailure(statusCode, headers, throwable, errorResponse);
            ShowAlert("Error", "error");
        }
    });

Laravel

if(Request::has('pic')){
        $files = Input::file('pic');
        //dd($files);
        $det_pic = [];
        foreach ($files as $file) {
            $filename = rand(11111,99999).'.'.$file->getClientOriginalExtension();
            $file->move('uploads', $filename);
            $det_pic[] = ['id_lapgiat'=>$id, 'file'=>$filename];
        }
        DB::table('bah_det_lapgiat_photo')->insert($det_pic);
        $output['has picture'] = true;
    }

谁能帮帮我?

【问题讨论】:

  • Samidh Sevenfoldisn 你能解决问题吗?我有同样的错误
  • 不,我不能,最后我迁移到了 codeigniter :D

标签: android asynchronous laravel-5 androidhttpclient


【解决方案1】:

使用 cURL 或 POSTMAN 客户端检查它是否在那里工作。如果没有,您可以将此示例代码用于 laravel。

//Checks if request has file or not
    if ($request->hasFile('pic')) {
//checks if file is uploaded or not
            if ($request->file('pic')->isValid()) {
                $extension = $request->file('pic')->getClientOriginalExtension();
                $imageName = str_random(60);
                $request->file('pic')->move(base_path() . 'file/save/location', $imageName.".".$extension);
                }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多