Laravel5.6 api picture upload error

problem description

upload the picture to the server in the form of api interface and report an error: Symfony Component HttpKernel Exception MethodNotAllowedHttpException

detailed error information:

the environmental background of the problems and what methods you have tried

I am using the laravel5.6 version. Uploading a .txt file can be successful, but this error will occur when uploading an image file, even if the image size is only more than a dozen KB.

related codes

/ / Please paste the code text below (do not replace the code with pictures)
routing code:
Route:: post ("uploadImg"," UploadImgController@uploadImg") in routes/api.php;
Route:: get ("upload"," FileController@upload");
Controller Code:
public function uploadImg (Request $request)

{
    if (!$request->hasFile("pic") || !$request->file("pic")->isValid()) {
        return response()->json(error("1"));
    }

    // 
    $extension = $request->file("pic")->extension();
    if (!in_array($extension, ["png", "jpg", "jpeg"])) {
        return response()->json(error("2"));
    }
    // 
    $size = $request->file->getClientSize();
    if ($size > 2048 * 1000) {
        return response()->json(error("3"));
    }
    // , 
    $path = $request->file("pic")->store("images","local");
    return response()->json(success(["src" => $path]));
}



public function upload(){
    return view("index");
}

template code:

< html >

<head>
    <title></title>
    <meta charset="utf-8">

</head>
<body>
<div class="container">
    <div class="panel-heading"></div>
    <form name="uploadImg" method="post" action="{{url("/api/uploadImg")}}" enctype="multipart/form-data"  >
        <label for="file"></label>
        <input name="pic" type="file" >
        <button type="submit" class="btn btn-primary"></button>
    </form>
</div>


</body>

< / html >

what result do you expect? What is the error message actually seen?

uploading txt files is fine, but uploading any image files will report this error.

Aug.12,2021

may be the error caused by enctype= "multipart/form-data" is that the request is illegal. Generally speaking, it is the problem on the HTTP request


. It is because of the permission problem of nginx. Just change the permission of nginx to that of www

.
Menu