首页 文章

请求 file()在 api Laravel 中为 null

提问于
浏览
0

我在 Laravel 的 API 中有一条路线用于 iOS 应用程序,它允许您上传我从本教程https://www.codetutorial.io/laravel-5-file-upload-storage-download/获得的图像,当我尝试上传文件时,它变为 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');

    }
}

路线:

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

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

用户不通过应用程序与网页进行交互

可能导致问题的其他信息是我使用 Postman 将图像上传到 laravel 应用程序(方法:POST,通过二进制部分)。

2 回答

  • 0

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

    enctype="multipart/form-data"
    
  • 0

    尝试使用 postman 中的form-data方法并添加参数作为文件类型。

    请记住,参数的键必须等于您尝试在后端获取的键。在你的情况下它是filefield

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

相关问题