php Laravel 5.1:将上传的文件保留为旧输入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33158328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:24:42  来源:igfitidea点击:

Laravel 5.1: keep uploaded file as old input

phplaravellaravel-5

提问by elton73

I'm using a form in Laravel 5.1 to post some text and to upload a file. It looks like this (simplified version):

我在 Laravel 5.1 中使用表单来发布一些文本并上传文件。它看起来像这样(简化版):

{!! Form::open(array('url' => 'foo/bar')) !!}
    {!! Form::text('image_name') !!}
    {!! Form::file('image') !!}
    {!! Form::submit('Submit!') !!}
{!! Form::close() !!}

The textfield is required, so I added a $validatorin my controller. If validation fails, the user is redirected back to the form. I use the withInput()method to repopulate the form so that the user doesn't have to fill it in again:

文本字段是必需的,所以我$validator在我的控制器中添加了一个。如果验证失败,用户将被重定向回表单。我使用该withInput()方法重新填充表单,以便用户不必再次填写:

if ($validator->fails()) {
    return redirect()->back()->withInput();
}

This will get the old input back for textfields, dropdowns etc. But if the user has uploaded a file, the file selection is gone when validation fails and has to be selected again. Is there any way in Laravel to remember file selection as old input?

这将恢复文本字段、下拉列表等的旧输入。但如果用户上传了文件,则验证失败时文件选择将消失,必须再次选择。Laravel 有没有办法将文件选择记住为旧输入?

Thanks!

谢谢!

回答by andrewtweber

No, the fileinput can't be prepopulated by Laravel or by any software. Your website (and any website) doesn't and shouldn't know the local path to the file. Imagine the security risks if they did! You could trick a user into uploading their SSH private key or something.

不,file输入不能由 Laravel 或任何软件预先填充。您的网站(和任何网站)不知道也不应该知道文件的本地路径。想象一下,如果他们这样做了会有什么安全风险!你可以欺骗用户上传他们的 SSH 私钥或其他东西。

What you need to do is process the uploaded file regardless, even if there are validation errors.

您需要做的是处理上传的文件,即使存在验证错误。

Then you could either store it in your database with a pendingstatus, or have some unique hashassigned to it, with a matching hashstored in the user's session. The point is to be able to identify incomplete uploads that belong to this specific user.

然后,您可以将其与pending状态一起存储在数据库中,或者hash为其分配一些唯一的内容,并将匹配项hash存储在用户会话中。关键是能够识别属于该特定用户的不完整上传。

Then when you display the form, retrieve those incomplete files either from the session or database, and display the thumbnail next to the file upload. This tells the user that they don't need to re-upload that file. Just make sure that they have a way to remove it as well in case they changed their mind.

然后,当您显示表单时,从会话或数据库中检索那些不完整的文件,并在文件上传旁边显示缩略图。这告诉用户他们不需要重新上传该文件。只要确保他们也有办法删除它,以防他们改变主意。

Once the form is submitted correctly then clear the session hashor update the database status to complete; whatever you need to do.

正确提交表单后,清除会话hash或将数据库状态更新为complete;无论你需要做什么。

回答by XaviQV

There seems to be a lot of questions around for the same issue (Form::file: How to repopulate with Input::old After Validation Errors and/or on Update?) and everyone with apparent knowledge says that is not possible.

对于同一个问题,似乎有很多问题(Form::file: How to repopulate with Input::old After Validation Errors and/or on Update?),每个有明显知识的人都说这是不可能的。

I would proppose a workaroundthat covers the following goals:

我会提出一个涵盖以下目标的解决方法

  • Keep form data and file input in case of failed Laravel validation
  • Use server side validation only. You don't needSHOULD NEVER USE javascript validation.
  • 保留表单数据和文件输入以防 Laravel 验证失败
  • 仅使用服务器端验证。您不需要应该永远不要使用 javascript 验证。

The keyword of it all is AJAX. I would do the following:

这一切的关键字是AJAX。我会做以下事情:

December 2017 update:Send all fields and attached files by a single AJAX POST

2017 年 12 月更新:通过单个 AJAX POST发送所有字段和附加文件

  1. Create an AJAX form (method GETPOST) that contains everything exceptincludingfile input. Remember that it requires to send the Laravel CSRF token.
  2. In the controller validation you need to make your responses (failed or success) to make a JSON output, so your AJAX send code can get the response in any case, and then you can show it in your browser
  1. 创建一个AJAX的形式(方法GET POST包含一切),除了包括文件输入。请记住,它需要发送Laravel CSRF 令牌
  2. 在控制器验证中,您需要做出响应(失败或成功)以生成 JSON 输出,这样您的 AJAX 发送代码在任何情况下都可以获得响应,然后您可以将其显示在浏览器中

This way you avoid to have files in any temporal state in your server.

通过这种方式,您可以避免在您的服务器中出现任何临时状态的文件。

A more complex but improved version of this process, if you are worried about used bandwidthand you expect having errors in your input fields will be very common, you may do two (or more) AJAX POST validations: The first one with the input fields only, and if it's ok, send all fields (input fields again, now including the attached files) to attempt validation again and do actions with all data in case of success.

此过程的一个更复杂但改进的版本,如果您担心使用的带宽并且您预计输入字段中的错误将非常常见,您可以执行两个(或多个)AJAX POST 验证:第一个带有输入字段只有,如果没问题,发送所有字段(再次输入字段,现在包括附加文件)以再次尝试验证并在成功的情况下对所有数据执行操作。

回答by Denis I

If your file is an image, you can create a hidden input and write there base64string there when the user uploads the image

如果您的文件是图像,您可以创建一个隐藏的输入并在用户上传图像时在那里写入base64字符串

{!! Form::open(array('url' => 'foo/bar')) !!}
{!! Form::text('image_name') !!}
{!! Form::hidden('base64_image') !!}
{!! Form::file('image') !!}
{!! Form::submit('Submit!') !!}
{!! Form::close() !!}