简体   繁体   中英

Request file() is null in api Laravel

I have a route in my API right in Laravel for an iOS app that lets you upload images that I got form this tutorial https://www.codetutorial.io/laravel-5-file-upload-storage-download/ and when I tried to upload the file it turns null.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Fileentry;
use Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\File;
use Illuminate\Http\Response;

class FileEntryController extends Controller
{


    public function add() {

        $file = Request::file('filefield');
        $extension = $file->getClientOriginalExtension();
        Storage::disk('local')->put($file->getFilename().'.'.$extension,  File::get($file));
        $entry = new Fileentry();
        $entry->mime = $file->getClientMimeType();
        $entry->original_filename = $file->getClientOriginalName();
        $entry->filename = $file->getFilename().'.'.$extension;

        $entry->save();

        return redirect('fileentry');

    }
}

Route:

$api = app('Dingo\Api\Routing\Router');


$api->version('v1', function ($api) {
    $api->post('fileentry/add',array(
        'as' => 'addentry',
        'uses' => 'App\Http\Controllers\FileEntryController@add'
    ));
}

the user doesn't interact with the web page is all through the app

Other information that maybe the cause of the problem is that i'm using Postman to upload the image to the laravel app (Method: POST, through the binary section).

在您的html表单中添加此属性

enctype="multipart/form-data"

Try using form-data method from postman and add a parameter as file type.

Keep in mind that the key of the parameter must be equal to the key you're trying to get in backend. In your case it is filefield

$file = Request::file('filefield');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM