如何在Laravel中读取FormData对象

时间:2022-10-26 09:16:33

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.

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

Like so:

像这样:

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

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

当form_data作为ajax调用中的数据发送时,我可以使用$ _FILES ['file_name'] ['name'];来获取PHP中的文件。

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

所以我在Laravel控制器方法中尝试了相同的逻辑。我试图在$ request对象中获取文件的名称但只得到null。

My controller method:

我的控制器方法:

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

    dd($file);
}

But when I dd the whole request, I see this weird object:

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

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 } ]

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}]

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

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

Thanks for any help

谢谢你的帮助

2 个解决方案

#1


4  

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

#2


0  

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。所以,我设置为假,没关系!

#1


4  

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

#2


0  

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。所以,我设置为假,没关系!