如何在 Laravel 中读取 FormData 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44354370/
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-09-14 16:04:52  来源:igfitidea点击:

how to read FormData object in Laravel

ajaxlaravelfilelaravel-5.4

提问by Awa Melvine

I am trying to ajax submit form to a Laravel 5 controller method. I understand that in php, you can define a FormData object, append the input fields to the object and send it to the server where you can now extract the values using the input field names.

我正在尝试将 ajax 提交表单提交给 Laravel 5 控制器方法。我知道在 php 中,您可以定义一个 FormData 对象,将输入字段附加到该对象并将其发送到服务器,您现在可以使用输入字段名称提取值。

Like so:

像这样:

var form_data = new FormData();
formdata.append('file_name', 'some_file_name_from_form.png');

When form_datais sent as the datain the ajax call, I can get the file in PHP by using the $_FILES['file_name']['name'];.

form_data作为dataajax 调用中的发送时,我可以使用 .php 文件在 PHP 中获取文件$_FILES['file_name']['name'];

So I tried this same logic in a Laravel controller method. I tried just to grab the name of the file in the $requestobject but only got null.

所以我在 Laravel 控制器方法中尝试了同样的逻辑。我试图只是获取$request对象中文件的名称,但结果为空。

My controller method:

我的控制器方法:

public function postImage(Request $request)
{
    $file = $request->get('file_name');

    dd($file);
}

But when I ddthe whole request, I see this weird object:

但是当我dd完成整个请求时,我看到了这个奇怪的对象:

array:1 [ "file_name" => UploadedFile {#199 -test: false -originalName: "work-fitness_00255959.png" -mimeType: "image/png" -size: 34215 -error: 0 #hashName: null path: "/tmp" filename: "phpVodsUg" basename: "phpVodsUg" pathname: "/tmp/phpVodsUg" extension: "" realPath: "/tmp/phpVodsUg" aTime: 2017-06-04 12:42:26 mTime: 2017-06-04 12:42:26 cTime: 2017-06-04 12:42:26 inode: 17573243 size: 34215 perms: 0100600 owner: 1000 group: 1000 type: "file" writable: true readable: true executable: false file: true dir: false link: false } ]

数组:1 [“file_name” => UploadedFile {#199 -test: false -originalName: “work-fitness_00255959.png” -mimeType: “image/png” -size: 34215 -error: 0 #hashName: null path: " /tmp" 文件名: "phpVodsUg" 基名: "phpVodsUg" 路径名: "/tmp/phpVodsUg" 扩展名: "" realPath: "/tmp/phpVodsUg" aTime: 2017-06-04 12:42:26 mTime: 2017-06 -04 12:42:26 cTime: 2017-06-04 12:42:26 inode: 17573243 size: 34215 perms: 0100600 owner: 1000 group: 1000 type: "file" writable: true 文件可读: true 可执行: false真目录:假链接:假}]

Please how do I get the image sent through FormData()object in Ajax through it's name?

请问如何通过FormData()Ajax中的对象的名称获取通过对象发送的图像?

Thanks for any help

谢谢你的帮助

回答by Sandeesh

You can do this.

你可以这样做。

// get the `UploadedFile` object
$file = $request->file('file_name');
$file = $request->file_name;

// get the original file name
$filename = $request->file('file_name')->getClientOriginalName();
$filename = $request->file_name->getClientOriginalName();

Check out the documentation for more information https://laravel.com/docs/5.4/requests#retrieving-uploaded-files

查看文档以获取更多信息https://laravel.com/docs/5.4/requests#retrieving-uploaded-files

The api methods available on the uploaded file http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

上传文件中可用的 api 方法http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

回答by Rafael Keller

The code bellow is working for me:

下面的代码对我有用:

$("#your_form").submit(function(e){
    e.preventDefault();
    var form = $(this);
    var url = form.attr("action");
    var data = new FormData(form[0]);
    $.ajax({
        url: url,
        type: 'POST',
        data: data,
        cache: false,
        processData: false,
        contentType : false,
        success: function (data) {
            console.log(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.log(jqXHR, textStatus, errorThrown);
        }
    });
}); 

my problem was in property contentType. So, I set as false and it's ok!

我的问题出在属性 contentType 上。所以,我设置为false就可以了!