【问题标题】:Request file() is null in api Laravel请求文件()在api Laravel中为空
【发布时间】:2018-04-21 12:00:03
【问题描述】:

我在 Laravel 的 API 中有一个用于 iOS 应用程序的路由,可让您上传我从本教程 https://www.codetutorial.io/laravel-5-file-upload-storage-download/ 获得的图像 当我尝试上传文件时,它变为空。

<?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,通过二进制部分)。

【问题讨论】:

    标签: php laravel api


    【解决方案1】:

    尝试使用邮递员的form-data 方法并添加一个参数作为文件类型。

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

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

    【讨论】:

      【解决方案2】:

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

      enctype="multipart/form-data"
      

      【讨论】:

      • 是的,这是一个应用程序,我的项目中没有任何视图,我希望能够在没有表单的情况下上传它
      猜你喜欢
      • 2019-05-20
      • 1970-01-01
      • 2018-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 2021-11-24
      相关资源
      最近更新 更多