upload images to web service with laravel

旧城冷巷雨未停 提交于 2019-12-08 11:13:01

问题


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]);
}

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


回答1:


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.

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.



来源:https://stackoverflow.com/questions/48649150/upload-images-to-web-service-with-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!