我试图在index.blade上显示上传的图像,但它没有显示,尽管<img>标签中的文件路径正确,而且我在控制台上判断了一下,它也得到了正确的路径. 图像存储控制器:

if($request->hasFile('profilepic')){
            $file = $request->file('profilepic');
            $profilepic=$file->getClientOriginalName();
            $file->storeAs('public/profile', $profilepic);
            $detail['profilepic'] = $profilepic;
        }

        if($request->hasFile('signature')){
            $file1 = $request->file('signature');
            $signature=$file1->getClientOriginalName();
            $file1->storeAs('public/sign', $signature);
            $detail['signature'] = $signature;
        }
        $detail->save();
        return response()->json([
            'status'=> 'success',
            'data' => $detail
        ]);

Img路径的索引.Blade

<td><img src="../storage/app/public/profile/{{ $detail->profilepic }}" width="100px"></td>
                <td><img src="storage/app/public/sign/{{ $detail->signature }}" width="100px"></td>

AJAX jQuery:

 $(document).on('submit','.create_form',function(event)
            {
                event.preventDefault();
                const fd = new FormData(this);
                $.ajax({
                    url: "{{ route('details.store') }}",
                    data: fd,
                    type: "POST",
                    dataType: "json",
                    contentType: false,
                    processData: false,
                    success: function(response)
                    {
                        $(".div3").load("{{ route('details.index') }}");
                    },
                    error: function(error)
                    {
                        console.log("Errors :",error);
                    }
                });
            });

Image of console getting correct url for image location

我还试着把.env档中的APP URL档放到<img src>档中,但不起作用.

推荐答案

我在下面列出了一些您可以try 验证/解决问题的内容.

  1. 确保图像正确存储在指定的存储目录中.判断public/profilepublic/sign目录以确认图像已保存在那里.

  2. 在Laravel中,不能直接访问存储在storage/app/public目录中的文件.您需要创建一个符号链接,以便可以从Web访问它们.在您的终端中运行以下命令以创建符号链接:

    php artisan storage:link
    

    此命令将创建从public/storagestorage/app/public的符号链接.确保已成功创建符号链接.

  3. 更新index.blade文件中的图像源路径以使用正确的URL.不使用相对路径,而是使用asset()助手函数根据符号链接生成正确的URL:

    <td><img src="{{ asset('storage/profile/' . $detail->profilepic) }}" width="100px"></td>
    <td><img src="{{ asset('storage/sign/' . $detail->signature) }}" width="100px"></td>
    

Let us know about the outcome of the above troubleshooting!

Php相关问答推荐

无法添加ProxyClass__setInitialized()上的忽略

MariaDB 11.3.2+PHP:服务器向客户端发送未知的字符集(0).请向开发人员报告

如何使用ms graph php sdk v2列出具有已知路径的DriveItem的子项

当主页和帖子的缩略图相同时,如何减少主页和帖子的缩略图

在WordPress中从CPT中删除插件

HTAccess重写一些URL,但将所有其他URL发送到另一个页面

未收到Strava WebHook事件数据

dyld[45923]:未加载库:/usr/local/opt/libavif/lib/libavif.15dylib

无法允许元素(img:'src')与symfony html-sanitizer(symfony6.3 php 8.2)

如何将不同的 Woocommerce 费用合并为一个名称?

用自定义代码替换 WooCommerce 单一产品简短描述

如何更新 Laravel Eloquent 列强制转换为集合

WooCommerce 按产品运输插件:仅收取较高的运输费用

在 groupBy laravel 5.7 中 Select 多列

从图像URL中获取文件扩展名?

Woocommerce注册中的自定义复选框验证错误提示问题

设置GA4 PHP API的setSubject以允许访问所有管理员账户属性

如何使用 WhatsApp Cloud API 向 WhatsApp 发送消息,而无需在发送消息之前注册收件人号码?

如何使用在产品快速编辑菜单前端的自定义框中 Select 的值更新 woocommerce 产品属性值?

我怎样才能在 .htaccess 中有 2 个参数?