从Laravel请求中在队列作业中访问文件。

huangapple go评论51阅读模式
英文:

access file from Laravel request inside a queue job

问题

以下是您要翻译的部分:

"Hello everyone I have a queued Laravel job that runs to insert some records in the database for an imported file by a user. but whenever I access the request object to get the uploaded file from inside the job I got null. however, the file is received in the controller normally. any idea?"

在 Laravel 中,我有一个被排队的作业(job),用于在用户导入的文件中插入一些记录到数据库中。但是每当我在作业中访问请求对象以获取上传的文件时,我得到的是 null。然而,在控制器中文件是正常接收的。有什么想法吗?

find the import method below in the controller

{
    Storage::makeDirectory('import_logs');
    $file        = request()->file('file');
    $fileName    = 'importlog_date_' . date('Y-m-d') . '-user_' . auth()->id() . '.xlsx';
    $logFile     = 'import_logs/' . $fileName;
    $readerTypes = ['xlsx' => Excel::XLSX, 'xls' => Excel::XLS, 'csv' => Excel::CSV];
    $link        = route('file.show', ['import_logs', $fileName]);

    try {
        ExcelFacade::import(new PointsImportHeading(), $file);
    } catch (\Exception $e) {
        return $this->returnBadRequest(config('point.error-codes.import-fail'), $e->getMessage());
    }

    (new PointsImport(auth()->user(), auth()->user()->account_id, $logFile))->queue(
        $file->getRealPath(),
        null,
        $readerTypes[request()->file('file')->getClientOriginalExtension()]
    )->chain([new AfterImportJob(auth()->id(), $logFile, $link)]);

    return $this->returnSuccess(trans('point::point.import-queued', ['module' => trans('point::point.point')]));
}

在控制器中找到以下的导入(import)方法。

find the getImportedFileContent method below in the job

{ 
     return Excel::toArray(new PointsImportHeading(), request()->file('file'));
}

在作业(job)中找到以下的 getUploadedFileContent 方法。

The problem in this part request()->file('file') always returns null.

问题出在这一部分:request()->file('file') 总是返回 null

英文:

Hello everyone I have a queued Laravel job that runs to insert some records in the database for an imported file by a user. but whenever I access the request object to get the uploaded file from inside the job I got null. however, the file is received in the controller normally. any idea?

find the import method below in the controller

 public function import(ImportPointRequest $request)
    {
        Storage::makeDirectory('import_logs');
        $file        = request()->file('file');
        $fileName    = 'importlog_date_' . date('Y-m-d') . '-user_' . auth()->id() . '.xlsx';
        $logFile     = 'import_logs/' . $fileName;
        $readerTypes = ['xlsx' => Excel::XLSX, 'xls' => Excel::XLS, 'csv' => Excel::CSV];
        $link        = route('file.show', ['import_logs', $fileName]);

        try {
            ExcelFacade::import(new PointsImportHeading(), $file);
        } catch (\Exception $e) {
            return $this->returnBadRequest(config('point.error-codes.import-fail'), $e->getMessage());
        }

        (new PointsImport(auth()->user(), auth()->user()->account_id, $logFile))->queue(
            $file->getRealPath(),
            null,
            $readerTypes[request()->file('file')->getClientOriginalExtension()]
        )->chain([new AfterImportJob(auth()->id(), $logFile, $link)]);

        return $this->returnSuccess(trans('point::point.import-queued', ['module' => trans('point::point.point')]));
    }

find the getImportedFileContent method below in the job

   protected function getUploadedFileContent(): array
    { 
         return Excel::toArray(new PointsImportHeading(), request()->file('file'));
    }

The problem in this part request()->file('file') always returns null.

答案1

得分: 1

你的方法不正确。在Laravel中,请求生命周期在请求发送到服务器时开始,当响应发送回用户的浏览器时结束。当你在Laravel中排队一个作业时,意味着该作业将稍后处理,甚至可能在不同的服务器上进行处理。当作业实际运行时,原始请求生命周期已经结束。因此,你无法在排队的作业内部访问请求数据。

如果你需要在排队的作业中使用上传的文件,你需要将上传的文件存储在作业可以访问的位置。这可以是在服务器文件系统上或在云存储服务上。

在你的控制器中,你已经暂时存储文件以便使用Excel处理:

$file = request()->file('file');

但是,你没有持久化文件,这就是为什么在作业运行时文件不可用的原因。你需要将文件存储在更持久的地方。

在将文件永久存储后,你可以从新位置读取文件。

英文:

Your approach is not true. In Laravel, the request lifecycle is started when a request comes to your server and it ends as soon as a response is sent back to the user's browser. When you queue a job in Laravel, it means that job will be processed later, perhaps even on a different server. When the job actually runs, the original request lifecycle is already over. Therefore, you cannot access the request data inside your queued job.

If you need to use the uploaded file in your queued job, you'll need to store the uploaded file in a location where your job can access it. This could be on your server's file system or on a cloud storage service.

In your controller, you're already storing the file temporarily to process it with Excel:

$file = request()->file('file');

However, you don't persist the file, which is why the file is not available when the job runs. You need to store the file somewhere more permanently.

After store your file permanently you can read the file from the new location.

huangapple
  • 本文由 发表于 2023年6月22日 00:20:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76525330.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定