flutter 从服务器下载图像抛出错误

hlswsv35  于 2023-06-30  发布在  Flutter
关注(0)|答案(1)|浏览(172)

我上传了我的laravel项目在托管服务器上,图像存储在laravel/public文件夹中,然后生成的url保存在我的数据库中,如:public/image.jpg,如何使用在线网站URL检索flutter中的图片??
在Flutter应用中:main_url=”http://mydomain.byethost7.com/api/“;
我上传了我的laravel项目托管服务器,图像存储在laravel/public文件夹,然后生成的URL保存在我的数据库中,如:public/image.jpg。我怎样才能检索flutter中的图片通过在线网站的网址?
在Flutter应用中:main_url=”http://mydomain.byethost7.com/api/“;

FutureBuilder(
           future: downloadImage(url),
            builder: (BuildContext context, AsyncSnapshot  snapshot) {

          if (snapshot.connectionState == ConnectionState.done &&
                  snapshot.hasData) {
                 return Container(
                    width: w,
                    height: h,
    child: Image.network(snapshot.data.toString(),fit: BoxFit.cover));
              }

             return Container();
            }
        )

Future<String> downloadImage(String url)async
{
  return await main_api_url + "getimage/$url";
}

在laravel api.php文件中是:

Route::get('getimage/{url}','App\Http\Controllers\Api\ImageController','get_image']);
in ImageController i have this function to return image as file:
public function get_image($url)
    {     
      ob_end_clean();

    return response()->file(public_path('storage/public/'.$url));

}

在laravel中我的ImageController类:

class ImageController extends Controller
{

public function save_images(Request $req)
{

if($req->hasFile($op['images']))
            {
              $images=$req->file($op['images']);
              foreach ($images as $image)
               {
                  $path=$image->store('public');
                  $img=new Image();
                  $img->url=$path;
                  $img->product_id=$product_id;
                  $img->title="";
                  $img->save();

          }

        }
}

}

在数据库图像表中:

但在慌乱中我得到了这个错误:

Exception caught by image resource service ================================================
The following _Exception was thrown resolving an image codec:
Exception: Invalid image data

When the exception was thrown, this was the stack: 
#0      _futurize (dart:ui/painting.dart:6637:5)
#1      ImageDescriptor.encoded (dart:ui/painting.dart:6494:12)
#2      instantiateImageCodecWithSize (dart:ui/painting.dart:2291:60)
#3      PaintingBinding.instantiateImageCodecWithSize (package:flutter/src/painting/binding.dart:182:15)
#4      NetworkImage._loadAsync (package:flutter/src/painting/_network_image_io.dart:153:22)
<asynchronous suspension>
Image provider: NetworkImage("http://murhafyildiz.byethost7.com/api/getimage/7zTKC23PVI5WTmGrbAIjnyQMFRMZEYUdzL9csuq9.png", scale: 1.0)
Image key: NetworkImage("http://murhafyildiz.byethost7.com/api/getimage/7zTKC23PVI5WTmGrbAIjnyQMFRMZEYUdzL9csuq9.png", scale: 1.0)
====================================================================================================
vaj7vani

vaj7vani1#

尝试更新返回图像的后端函数:

public function get_image($filename)
            {
                // ob_end_clean(); NOT NEEDED
                $filePath = storage_path('public/' . $filename);

                if (file_exists($filePath)) {
                    return response()->file($filePath);
                } else {
                    abort(404);
                }
            }

相关问题