【问题标题】:How to upload multiple image using laravel api?如何使用 laravel api 上传多张图片?
【发布时间】:2019-02-07 03:32:13
【问题描述】:

我仍在学习使用 Laravel 5.7 创建用于上传多张图片的 API。当我尝试通过邮递员上传一张图片时,该图片可以存储在数据库中,但是当我尝试上传两张或更多图片时,它不能存储在数据库中。我在邮递员中没有收到错误,但在邮递员重定向到我的登录页面中预览。

这是我的控制器:

         $this->validate($request, [
        'filename.*' => 'image|mimes:jpeg,png,jpg|max:2048'
        ]);

        if($request->hasfile('filename'))
         {
            $images=array();
            foreach($request->file('filename') as $image)
            {
                $photo_name = time().'_'.$request->input('fa_transaction_id').'.png';
                $destinationPath = public_path('/uploads/workphotos');
                $image->move($destinationPath, $photo_name);
                $img_url = asset('/uploads/workphotos/'.$photo_name);

                $data = new FA_Transaction_photo();
                $data->fa_transaction_id = $request->input('fa_transaction_id');
                $data->user_id = $request->input('userid');
                $data->photo_name = $photo_name;
                $data->photo_url = $img_url;
                $data->save();
                array_push($images,$img_url);
            }

有人可以帮助我吗?或者任何人都可以教我如何做到这一点? 谢谢

【问题讨论】:

  • 你可以转储$request->file('filename') 以确保它有多个文件。
  • 我已经转储了 $request->file('filename'),但它会自动重定向到登录页面
  • 你好@RahmatEffendi,你能给我看一下邮递员的屏幕截图吗,你必须发送文件名[]作为键而不是多张图片的文件名。
  • 同样你在for循环中返回response,它会在上传第一张图片后返回,所以将return response()->json()放在for循环之外。
  • @RaviGaudani 我更新了我的问题以放置邮递员的屏幕截图,并且我已将 response()->json() 放在 for 循环之外,但我得到了相同的结果,它重定向到我的登录页面跨度>

标签: php laravel laravel-5 php-7


【解决方案1】:

你犯了小错误,再次检查你的代码

return response()->for 循环中的json

所以只有第一张图片被上传和存储,然后请求返回响应

如下修改你的代码:

use Illuminate\Support\Facades\Storage;

$validator = Validator::make($request->all(),
   ['userid'=>'required',
    'fa_transaction_id'=>'required',
    'filename' => 'array',
    'filename.*' => 'image|mimes:jpeg,png,jpg|max:2048'
   ]);

   if($validator->fails())
   {
    return response()->json(['message'=>$validator->errors()->all(),'success'=>0]);
   }

    $trans = FA_transaction::where('fa_transaction_id', $request->fa_transaction_id)->first();

if($trans){
     $this->validate($request, [
    'filename.*' => 'image|mimes:jpeg,png,jpg|max:2048'
    ]);

    if($request->hasfile('filename'))
     {
        $images=array();
        foreach($request->file('filename') as $image)
        {
            $photo_name = time().'_'.$request->input('fa_transaction_id').'.png';
            $destinationPath = public_path('/uploads/workphotos');
            $image->storeAs($destinationPath,$filename);
            $img_url = asset('/uploads/workphotos/'.$photo_name);

            $data = new FA_Transaction_photo();
            $data->fa_transaction_id = $request->input('fa_transaction_id');
            $data->user_id = $request->input('userid');
            $data->photo_name = $photo_name;
            $data->photo_url = $img_url;
            $data->save();
            array_push($images,$img_url);
        }

        return response()->json(['message'=>'Upload Successfully ','success'=>1,'images'=>$images]);

     }else{
      return response()->json(['message'=>'No Files ','success'=>0]);

   }
 }

在post man中添加这个标题

内容类型:多部分/表单数据

【讨论】:

  • 你好@SaurabhMistry,我试过了,但它不起作用,它仍然重定向到登录页面
  • 显示您的路线代码,并在您的问题中添加控制器的全部功能
  • 我已使用您的邮政编码@SaurabhMistry 更新了我的问题
  • 我在 postman { "message": [ "The filename.0 failed to upload." 中收到此错误。 ], "成功": 0 }
  • @RahmatEffendi,我认为是验证错误,请尝试更新验证
猜你喜欢
  • 2020-03-20
  • 2017-07-27
  • 1970-01-01
  • 2015-04-23
  • 2020-10-07
  • 2020-12-05
  • 2021-02-27
  • 2021-06-30
  • 1970-01-01
相关资源
最近更新 更多