使用laravel将图像上传到Web服务 [英] upload images to web service with laravel

查看:102
本文介绍了使用laravel将图像上传到Web服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel 5.4和Guzzle将文件和图像上传到另一台服务器.

I'm using Laravel 5.4 and Guzzle to upload files and images to another server.

我在客户端/服务器端使用以下方法

I am using the following method on the client/server side

public function store (Request $request)
{
    $dataForm = $request->all();    
    $Client = new Client();
        
    $response = $Client->post('http://localhost/WebService/public/api/client',
    [
       'multipart' =>
        [
            [
                'name' => 'UploadIMG',
                'filename' => 'image_org',
                'Mime-Type' => 'image_mime',
                'contents' => $ request->file('UploadIMG'),
            ],

        ]
            
    ]);
    $ response = $ response-> getBody () -> getContents ();
    return response () -> json (['error' => $ response]);
}

在文档的内容部分是'contents'=> fopen('/path/to/file','r')但我不想保存图像,但已经获得了能够发送的请求, 这怎么可能? 我使用的方式是Web服务中的文件损坏. 我与邮递员进行了一些测试,所以问题出在枪口中

In the documentation part of the contents is 'contents' => fopen ('/ path / to / file', 'r') but I do not want to save the image but already get the request to be able to send, how is this possible? the way I'm using comes a corrupted file in the web service. I ran some tests with postman, so the problem is in the guzzle

推荐答案

使用当前的Guzzle代码,您正在传递作为Illuminate\Http\UploadedFile实例的$request->file('UploadIMG').对于HTTP请求的内容",您需要将实际文件本身传递给它.

With your current code for Guzzle, you're passing the $request->file('UploadIMG') which is an instance of Illuminate\Http\UploadedFile. For the 'contents' of the HTTP request, you need to pass the actual file itself into it.

由于UploadedFile最终扩展了SplFileInfo,所以我猜测是使用其$request->file('UploadIMG')->openFile()方法.如果您不能从那里阅读它,那么我的第二个猜测就是暂时保存它,然后阅读.

Because the UploadedFile eventually extends SplFileInfo, my guess is to use its method of $request->file('UploadIMG')->openFile(). If you can't read it from there, then my second guess would be to save it temporarily then read that.

这篇关于使用laravel将图像上传到Web服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆