使用laravel 8中的HTTP guzzle通过API将客户端上的多个文件上传到服务器 [英] Upload multiple files from Client to Server via API using HTTP guzzle in laravel 8

查看:258
本文介绍了使用laravel 8中的HTTP guzzle通过API将客户端上的多个文件上传到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我由邮递员进行了测试,可以将文件附加到字段fileupload [0](请参见下面的屏幕截图)中,该文件可以正常工作,添加了请求,并且 fileupload 也已上传到服务器.

但是我想通过我的laravel HTTP客户端调用该上传文件api

我尝试了以下代码

  $ result = Http :: attach('附件',$ request->文件上传)-> asForm()-> post('http://192.168.1.100/api/request/store',$ form_param); 

我在表单上传中的控件:

 <输入类型=文件"name =" fileupload []"多个 

但是看来API服务器没有从客户端获取我的请求文件.

那么如何使用laravel HTTP通过客户端上传文件(或多个文件)?

解决方案

您需要更改的内容很少.

这是在guzzle文档中写的一行.由于Http Client是guzzle的包装类,因此它也适用于它.

multipart不能与form_params选项一起使用.您将需要使用其中一个.将form_params用于application/x-www-form-urlencoded请求,将multipart用于multipart/form-data请求.

同样如上图所示,您的数据内容类型是 form-data 而不是 application/x-www-form-urlencoded .

因此您不能使用 asForm().它将内容类型在内部设置为 application/x-www-form-urlencoded .

attach()函数在内部添加了 asMultipart(),因此您无需添加它.

第二,为了传递多个附件,您需要将数组作为第一个参数传递给attach()函数.为此

  if($ request-&>; hasFile('fileupload')){$ names = [];foreach($ request-> file('fileupload')as $ file){if(file_exists($ file)){$ name = $ file-> getClientOriginalName();$ names [] = $ name;}}} 

现在将此数组 $ names 传递给attach方法,

  $ result = Http :: attach($ names,$ request-> file('fileupload'))-> post('http://192.168.1.100/api/request/store',$ form_param); 

如果您在内部将数组作为第一个参数传递,则此函数是递归的,并会自行调用以上传所有文件.


尽管这不是问题的一部分,但我建议您以下面的给定格式调用http请求.

 试试{$ result = Http :: attach($ names,$ request-> file('fileupload'))-> post('http://192.168.1.100/api/request/store',$ form_param);$ result-> throw();} catch(\ Illuminate \ Http \ Client \ RequestException $ e){\ Log :: info($ e-> getMessage());//相应地处理您的异常} 

因为它永远不能保证响应总是可以的.

I tested by postman to attach file with field fileupload[0](see screenshot below) that works fine, request is added and fileupload also uploaded to server.

But I want to call that upload file api by my laravel HTTP Client

I have tried below code

$result = Http::attach(
            'attachment', $request->fileupload
        )->asForm()->post('http://192.168.1.100/api/request/store', $form_param);

my control in form upload:

 <input type="file" name="fileupload[]" multiple>

But it seem like API server does not get my request file from the client side.

So how to upload file(or multiple files) via client using laravel HTTP?

解决方案

There are few things you need to change.

This is a line written in documentation of guzzle. As Http Client is the wrapper class of guzzle it applies on it as well.

multipart cannot be used with the form_params option. You will need to use one or the other. Use form_params for application/x-www-form-urlencoded requests, and multipart for multipart/form-data requests.

Also as you shown in your screenshot of postman above as well, that you are content-type of your data is form-data not application/x-www-form-urlencoded.

So you cannot use asForm(). It internally sets content type to application/x-www-form-urlencoded.

The attach() function internally adds asMultipart() so you don't need to add it.

Secondly, In order to pass multiple attachment you need to pass array as first argument to the attach() function. For that

    if($request->hasFile('fileupload')) {
        $names = [];
        foreach ($request->file('fileupload') as $file) {
            if(file_exists($file)){
                $name= $file->getClientOriginalName();
                $names[] = $name;
            }
        }
    }

Now pass this array $names to the attach method,

$result = Http::attach(
            $names, $request->file('fileupload')
        )->post('http://192.168.1.100/api/request/store', $form_param);

If you pass array as first argument internally this function is recursive and calls itself to upload all files.


Though it is not part of question but I recommend calling http requests in the given format below.

   try{
   $result = Http::attach(
            $names, $request->file('fileupload')
        )->post('http://192.168.1.100/api/request/store', $form_param);
   $result->throw(); 
} catch(\Illuminate\Http\Client\RequestException $e){
   \Log::info($e->getMessage());
   // handle your exception accordingly
}

As it is never a guarantee that the response will always be ok.

这篇关于使用laravel 8中的HTTP guzzle通过API将客户端上的多个文件上传到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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