laravel-chunk-upload icon indicating copy to clipboard operation
laravel-chunk-upload copied to clipboard

The request is missing a file. The request is missing a file at UploadController.php:30

Open imonoid opened this issue 6 years ago • 4 comments

Good day. I am getting that error lately and none of my uploads are going through.
I am not sure what is causing this but I am using the saveFileToS3.

Any of you know what is causing this? I am not sure if this is an S3 issue or just a config issue in PHP/Laravel as I was able to upload files before successfully in S3.

imonoid avatar Jul 29 '19 13:07 imonoid

How does your UploadController looks like? This package does not contain that file so it is hard to guess what is happening in your code.

nerg4l avatar Aug 18 '19 10:08 nerg4l

@imonoid If you are using default code, the name of the file field must be set to file. Please check your form + JS for correct name (or update the the code new FileReceiver("file")

pionl avatar Nov 05 '19 21:11 pionl

i have this problem too in default codes and test.html of Resumable example With Laravel 8 The request is missing a file in file

`<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller; use Illuminate\Http\Request; use Pion\Laravel\ChunkUpload\Exceptions\UploadFailedException; use Storage; use Illuminate\Http\UploadedFile; use Pion\Laravel\ChunkUpload\Exceptions\UploadMissingFileException; use Pion\Laravel\ChunkUpload\Handler\AbstractHandler; use Pion\Laravel\ChunkUpload\Handler\HandlerFactory; use Pion\Laravel\ChunkUpload\Receiver\FileReceiver;

class UploadController extends Controller { //

public function upload(Request $request)
{

    // create the file receiver
    $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));

    // check if the upload is success, throw exception or return response you need
    if ($receiver->isUploaded() === false) {
        throw new UploadMissingFileException();
    }

    // receive the file
    $save = $receiver->receive();

    // check if the upload has finished (in chunk mode it will send smaller files)
    if ($save->isFinished()) {
        // save the file and return any response you need, current example uses `move` function. If you are
        // not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
        return $this->saveFile($save->getFile());
    }

    // we are in chunk mode, lets send the current progress
    /** @var AbstractHandler $handler */
    $handler = $save->handler();

    return response()->json([
        "done" => $handler->getPercentageDone(),
        'status' => true
    ]);
}

/**
 * Saves the file to S3 server
 *
 * @param UploadedFile $file
 *
 * @return JsonResponse
 */
protected function saveFileToS3($file)
{
    $fileName = $this->createFilename($file);

    $disk = Storage::disk('s3');
    // It's better to use streaming Streaming (laravel 5.4+)
    $disk->putFileAs('photos', $file, $fileName);

    // for older laravel
    // $disk->put($fileName, file_get_contents($file), 'public');
    $mime = str_replace('/', '-', $file->getMimeType());

    // We need to delete the file when uploaded to s3
    unlink($file->getPathname());

    return response()->json([
        'path' => $disk->url($fileName),
        'name' => $fileName,
        'mime_type' => $mime
    ]);
}

/**
 * Saves the file
 *
 * @param UploadedFile $file
 *
 * @return JsonResponse
 */
protected function saveFile(UploadedFile $file)
{
    $fileName = $this->createFilename($file);
    // Group files by mime type
    $mime = str_replace('/', '-', $file->getMimeType());
    // Group files by the date (week
    $dateFolder = date("Y-m-W");

    // Build the file path
    $filePath = "upload/{$mime}/{$dateFolder}/";
    $finalPath = storage_path("app/" . $filePath);

    // move the file name
    $file->move($finalPath, $fileName);

    return response()->json([
        'path' => $filePath,
        'name' => $fileName,
        'mime_type' => $mime
    ]);
}

/**
 * Create unique filename for uploaded file
 * @param UploadedFile $file
 * @return string
 */
protected function createFilename(UploadedFile $file)
{
    $extension = $file->getClientOriginalExtension();
    $filename = str_replace("." . $extension, "", $file->getClientOriginalName()); // Filename without extension

    // Add timestamp hash to name of the file
    $filename .= "_" . md5(time()) . "." . $extension;

    return $filename;
}

} `

its my full controller Codes

sh0beir avatar Mar 05 '22 11:03 sh0beir

  • your JS file?
  • if you dd the request, can you see the file in $files property of the request?

pionl avatar Mar 21 '22 13:03 pionl