Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Multer req.file未使用表单定义,但未使用邮递员表单数据_Javascript_Node.js_Laravel - Fatal编程技术网

Javascript Multer req.file未使用表单定义,但未使用邮递员表单数据

Javascript Multer req.file未使用表单定义,但未使用邮递员表单数据,javascript,node.js,laravel,Javascript,Node.js,Laravel,当试图从前端上传图像时,我一直从api获取req.file未定义。我使用Laravel的guzzle http发布数据。然而,当我使用邮递员的表单数据时,图像被成功上传,因此我认为这可能与前端的某些内容有关 如果你能给我指出正确的方向,那会很有帮助 这是我到目前为止得到的 节点js控制器 exports.post_image=(请求、分辨率、下一步)=>{ console.log(req.file);//输出未定义 //验证文件是否存在 如果(!req.file)返回res.status(400

当试图从前端上传图像时,我一直从api获取req.file未定义。我使用Laravel的guzzle http发布数据。然而,当我使用邮递员的表单数据时,图像被成功上传,因此我认为这可能与前端的某些内容有关

如果你能给我指出正确的方向,那会很有帮助

这是我到目前为止得到的

节点js控制器

exports.post_image=(请求、分辨率、下一步)=>{
console.log(req.file);//输出未定义
//验证文件是否存在
如果(!req.file)返回res.status(400).json({message:'Please upload a image!'});}
节点js路由

router.post('/',upload.single('image'),ImageController.post_image);
Laravel控制器

公共函数存储(请求$Request)
{
$validatedData=Validator::make($request->all()[
“图像”=>“必需”|图像:jpeg、png、jpg |最大值:2048”,
])->验证();
如果($request->hasFile('image')){
$image=$validatedData['image'];
}
//dd($image);//返回文件及其属性
$url='1http://localhost:3000/upload';
$response=Http::post($url[
'image'=>$image
]);
dd(json_decode($response));//响应为“请上传图像!”
$response\u decode=json\u decode($response);
$message=$response\u decode->message;
session()->flash('success',$message);
return redirect()->action('ImageController@index');
}
Html页面


@csrf
@方法('PUT')
形象
上传
如果要将文件作为多部分请求发送,则应在发出请求之前调用attach方法

您需要更改某些行, 第一个变化

 if ($request->hasFile('image')) {
      $image = $request->file('image');
 }

那么第二,

 $url = 'http://localhost:3000/upload';
 $photo = fopen($image, 'r');

 $image_name = 'image' . $image->getClientOriginalExtension();
 $response = Http::attach(
     'image', $photo, $image_name
 )->post($url);

 dd($response->json(), $response->status());
你可以从中读到更多


方法2: 您可以直接使用Guzzle HTTP客户机,而不是使用HTTPClient(Guzzle HTTP客户机周围的表现性、最小API)


你的内容类型标题是什么?请告诉我这是否有效,我也有其他方法。我尝试了上面的建议并查看了文档,但仍然无效。如果可能,请提供其他方法。我添加了Guzzle的直接用法,我直接使用Guzzle,因此我知道它有效,但是为什么HTTP客户端不能工作我必须尝试一下,你可以看到这一点,尽管它被合并了,所以不应该有任何问题,我将尝试&informOk让我检查一下,使用你提供的第二个代码成功上传了图像,但是当我尝试向它添加其他表单字段时,我得到一个错误声明:无效资源类型:数组。你知道怎么解决这个问题吗?
try{
    $url = 'http://localhost:3000/upload';
    if (!empty($image)){
        $guzzleResponse = $client->post($url, [
                        'multipart' => [
                            [
                                'name' => 'image',
                                'contents' => fopen($image, 'r')
                            ] //,
                            /** for other parameters
                            [
                                 'name' => 'description'
                                 'contents' => 'image about cat'
                            ]  //put ',' and similarly add other parameters
                             */
                         ] // ,
                         // 'headers' => $headers  you can add headers here(commented as you have not added any headers in your code snippet)
                    ]);
        if ($guzzleResponse->getStatusCode() == 200) {
            $response = json_decode($guzzleResponse->getBody(), true);  
        }
      }
    }catch (RequestException $e) {
        // you can catch here 400 response errors and 500 response errors
        // see this https://stackoverflow.com/questions/25040436/guzzle-handle-400-bad-request/25040600
    } catch(Exception $e){
        //other errors 
    }
}