简体   繁体   English

Laravel - 删除表单中上传的文件

[英]Laravel - Remove uploaded file in a form

This is my very first message here and I apologize if the question has been asked before.这是我在这里的第一条信息,如果之前有人问过这个问题,我深表歉意。

Also, sorry for my english, I'm french...另外,对不起我的英语,我是法国人......

I'm building a Laravel app and in one of my forms, I give the user the possibility to upload a file but it's not required.我正在构建一个 Laravel 应用程序,在我的 forms 之一中,我让用户可以上传文件,但这不是必需的。

However, once it's uploaded, there's no way to delete it in my Edit or Upload route.但是,一旦上传,就无法在我的编辑或上传路线中删除它。

Let's say a user uplaods a file and changes their mind, how can they remove it?假设用户上传了一个文件并改变了主意,他们如何删除它?

Here's a screenshot of the form:这是表单的屏幕截图:

Upload file field上传文件字段

Here's my controller:这是我的 controller:

public function update(Request $request, $id)
{
    $this->validate($request, [
        'title' => 'required|string',
        'timing' => 'required',
        'file' => 'file',
        'lesson_id' => 'required',
        'video' => 'required',
        'description' => 'required'
    ]);

    $lecture = Lecture::findOrFail($id);

    $lecture->title = $request['title'];
    $lecture->timing = $request['timing'];
    $lecture->lesson_id = $request['lesson_id'];
    $lecture->video = $request['video'];
    $lecture->description = $request['description'];

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

    if ($request->hasFile('file')) {
        $fileName = $file->getClientOriginalName();
    $fileExtension = $file->getClientOriginalExtension();

    $destinationPath = public_path('chapitres-files');

    $file->move($destinationPath, $fileName);
    $lecture->file = 'chapitres-files/' . $fileName;
    }

So, can someone explain me what to do exactly in my controller, in my view file and also in my web.php?那么,有人可以解释我在我的 controller、我的视图文件以及我的 web.php 中究竟该怎么做吗?

Thank you very much!非常感谢!

Gastono加斯托诺

You have 2 main scenarios to cover:您需要涵盖 2 个主要场景:

  1. A user adds file to the form but the decides to remove it before upload用户将文件添加到表单,但决定在上传之前将其删除
  2. A user has already uploaded a file and now wants to delete it用户已经上传了一个文件,现在想要删除它

Scenario 1: Removing a file from a form object场景 1:从表格中删除文件 object

I will point you at this question as a starting point as I am not a Javascript expert我会以这个问题为起点,因为我不是 Javascript 专家

How do I remove a file from the FileList 如何从 FileList 中删除文件


Scenario 2: Removing a file from storage场景 2:从存储中删除文件

As mentioned in the comments, in this scenario you will want to remove the file as a seperate action rather than as part of your Edit / Update code.如评论中所述,在这种情况下,您将希望将文件作为单独的操作而不是作为编辑/更新代码的一部分来删除。

This keeps your destructive actions seperate from your constructive actions这使您的破坏性行为与您的建设性行为分开

The way I would approach this is:我的方法是:

  1. Create an API Controller for your files ( FileController ) with a destroy method on it为您的文件( FileController )创建一个 API Controller 并使用destroy方法
  2. Create an API Route for your files ( api/files/destroy/{file_id} )为您的文件创建一个 API 路由 ( api/files/destroy/{file_id} )
  3. Use javascript to fire an AJAX request to your API Route with the identifier for the file you want to delete使用 javascript 向您的 API 路由发出 AJAX 请求,并带有您要删除的文件的标识符

Note: I would suggest also adding an confirmation step to your "delete" button (like a modal or alert) to make the user confirm that they do indeed want to delete the file注意:我建议还向您的“删除”按钮(如模式或警报)添加一个确认步骤,以使用户确认他们确实想要删除文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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