将图像文件从Android设备上传到Laravel API

时间:2019-07-19 17:00:05

标签: android laravel react-native

我使用React-Native构建应用程序,并在Laravel中为该项目创建了API。现在,当我使用生成的apk文件在Android手机上安装应用程序时,无法将图像文件保存到S3存储中。

当我在ios和android模拟器上本地工作并且还使用Postman发送文件时,一切正常,但是在物理android设备中,它无法保存图像,也不会捕获任何错误。

有我的控制器方法代码:

public function updatePhoto(Request $request)
    {
        try{
            $userEmail = $request->userEmail;
            $filename = 'userPhotos/' . time() . '-' . $request->fileName . ".jpg";

            Storage::disk('s3')->put($filename, file_get_contents($request->file));
            Storage::disk('s3')->setVisibility($filename, 'public');

            $url = Storage::disk('s3')->url($filename);

            $updateUserPhoto = DB::table('users')
                    ->where('email', $userEmail)
                    ->update(['photo_path' => $url]);

            $user = DB::table('users')
                    ->where('email', $userEmail)->get();

            return response()->json(['status' => 'OK', 'result' => $user]); 
        }catch(\Exception $e){
            $user = DB::table('users')
                    ->where('email', $userEmail)->get();

            $this->storeErrorLog($user->id, '/updatePhoto', $e->getMessage());

            return response()->json(['status' => 'ERR', 'result' => $e->getMessage()]); 
        }
    }

有人对Laravel API有类似的问题吗?也许这是一些android图像数据格式?非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

有我的laravel_dir / storage / logs / laravel.log:

[2019-07-20 06:19:41] production.ERROR: Property [id] does not exist on this collection instance. {"exception":"[object] (Exception(code: 0): Property [id]$
[stacktrace]
#0 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/app/Http/Controllers/UserController.php(213): Illuminate\\Support\\Collection->__get('id')
#1 [internal function]: App\\Http\\Controllers\\UserController->updatePhoto(Object(Illuminate\\Http\\Request))
#2 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): call_user_func_array(Array, Array)
#3 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(45): Illuminate\\Routing\\Con$
#4 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Route.php(212): Illuminate\\Routing\\ControllerDispatc$
#5 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Route.php(169): Illuminate\\Routing\\Route->runControl$
#6 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Router.php(679): Illuminate\\Routing\\Route->run()
#7 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Routing\\Router->Illumin$
#8 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\\Rou$
#9 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Routing\\Middleware\\S$
#10 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#11 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Middleware/ThrottleRequests.php(58): Illuminate\\Rout$
#12 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Routing\\Middleware\\$
#13 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#14 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Il$
#15 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Router.php(681): Illuminate\\Pipeline\\Pipeline->then$
#16 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Router.php(656): Illuminate\\Routing\\Router->runRout$
#17 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Router.php(622): Illuminate\\Routing\\Router->runRout$
#18 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Router.php(611): Illuminate\\Routing\\Router->dispatc$
#19 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\\Routing\\Router-$
#20 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\\Foundation\\Http\\Kerne$
#21 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/fideloper/proxy/src/TrustProxies.php(57): Illuminate\\Routing\\Pipeline->Illuminate\\Routing\\$
#22 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Fideloper\\Proxy\\TrustProxies->h$
#23 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#24 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illumin$
#25 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Mid$
#26 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#27 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(31): Illumin$
#28 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Mid$
#29 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#30 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illumina$
#31 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Mid$
#32 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#33 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(62): I$
#34 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(151): Illuminate\\Foundation\\Http\\Mid$
#35 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\\Pipeline\\Pipeline->Ill$
#36 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\\Routing\\Pipeline->Il$
#37 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\\Pipeline\\Pipeli$
#38 /usr/home/RadoszSzymon/domains/e-mamy.pl/emamyAPI/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\\Foundation\\Http$
#39 /usr/home/RadoszSzymon/domains/e-mamy.pl/public_html/index.php(55): Illuminate\\Foundation\\Http\\Kernel->handle(Object(Illuminate\\Http\\Request))
#40 {main}