【问题标题】:Cant Upload Multiple Images In Laravel jQuery AJAX无法在 Laravel jQuery AJAX 中上传多个图像
【发布时间】:2021-07-18 22:45:53
【问题描述】:

我在尝试上传图像文件时遇到了很多天。我无法使用以下代码上传多个图像文件:

控制器

if ($request->TotalImages > 0) {

            for ($x = 0; $x < $request->TotalImages; $x++) {

                if ($request->hasFile('images' . $x)) {
                    $file      = $request->file('images' . $x);

                    $path = $file->store('public/product_images/');
                    $name = $file->getClientOriginalName();

                    $insert[$x]['name'] = $name;
                    $insert[$x]['path'] = $path;
                }
            }
        }

        $productId = DB::table('products')->insertGetId(
            [
                'product_photo' => $insert
            ]
        );

查看

<input id="images" name="images[]" type="file" multiple
                            class="form-control {{ $errors->first('images') ? 'is-invalid' : '' }}"
                            data-iconName="fa fa-upload" data-overwrite-initial="false">
                        <br>
var formData = new FormData();
let TotalImages = $('#images')[0].files.length; //Total Images
        let images = $('#images')[0];
        for (let i = 0; i < TotalImages; i++) {
            formData.append('images' + i, images.files[i]);
        }
        formData.append('TotalImages', TotalImages);

        $.ajax({
            url: "{{ route('products.store') }}",
            method: 'post',
            enctype: 'multipart/form-data',
            cache: false,
            data: formData,
            contentType: false,
            processData: false,
            dataType: 'JSON',
            async: true,
            headers: {
                'Content-Type': undefined,
            },
            xhr: function() {
                myXhr = $.ajaxSettings.xhr();
                return myXhr;
            },
            
        }); //ajax

它总是显示错误:

图片字段是必填项。

但是单次上传就可以了!

【问题讨论】:

    标签: jquery ajax laravel laravel-5


    【解决方案1】:

    您应该在插入数据库之前对变量进行编码。并且不要在一开始就验证图像。只有在您检查了 if ($request-&gt;TotalImages &gt; 0) 之后才能验证图像,如果是TotalImages === 0,则验证图像。

    if ($request->TotalImages > 0) {
    
            for ($x = 0; $x < $request->TotalImages; $x++) {
    
                if ($request->hasFile('images' . $x)) {
                    $file      = $request->file('images' . $x);
    
                    $path = $file->store('public/product_images/');
                    $name = $file->getClientOriginalName();
    
                    $insert[$x]['name'] = $name;
                    $insert[$x]['path'] = $path;
                }
            }
    
    $productId = DB::table('products')->insertGetId(
            [
                'product_photo' => json_encode($insert)
            ]
        );
    
        } else { $imageValidator = \Validator::make($request->all(), [
                    'images' => 'required|file|mimes:jpeg,png,jpg,gif,svg',
                ]);
                return response()->json(['errors' => $imageValidator->errors()->all()]);
        }
    

    【讨论】:

      猜你喜欢
      • 2021-12-07
      • 2020-12-01
      • 2019-12-01
      • 2021-10-03
      • 1970-01-01
      • 2015-01-18
      • 2021-07-14
      • 2019-01-11
      • 1970-01-01
      相关资源
      最近更新 更多