【问题标题】:Accessing FormData file on the PHP server (symfony)访问 PHP 服务器 (symfony) 上的 FormData 文件
【发布时间】:2015-12-07 00:10:38
【问题描述】:

我的问题是,在使用 FormData 发出 ajax 请求后,我不知道如何在服务器端获取文件和文件名。让我补充一点,另一方面它适​​用于字符串。我还通过 FireBug 看到了请求中的文件和详细信息。

这是我的代码:

JavaScript:

var form = document.getElementById('file-form');
var fileSelect = document.getElementById('file-select');
var uploadButton = document.getElementById('upload-button');


// Get the selected files from the input.
var files = fileSelect.files;
// Create a new FormData object.
var formData = new FormData();

var myArray = [];
// Loop through each of the selected files.
for (var i = 0; i < files.length; i++) {
var file = files[i];
console.log(file.name);
myArray[i] = file.name;

// Check the file type.
if (!file.type.match('image.*')) {
continue;
}

 // Add the file to the request.
formData.append('photos[]', file, file.name);
}


var xhr = new XMLHttpRequest();
// Open the connection.
xhr.open('POST',
'http://localhost/amsprojektgit/amsprojekt/admin/web/fileupload', true);

xhr.onload = function () {
if (xhr.status === 200) {
// File(s) uploaded.
alert('Files uploaded');
} else {
alert('An error occurred!');
}
};

xhr.send(formData);

服务器端 PHP(Symfony2 容器)

public function fileUpload(Request $request)
{

$upload = $_POST;
$logger = $this->get('logger');
$logger->info($upload);
$logger->error('');

die();
}

通过 FireBug 的响应如下所示:

-----------------269272101016568 内容处置:表单数据;名称="照片";文件名="elektpolaniec2.jpg" 内容类型:image/jpeg

ÿØÿà�JFIF��H�H��ÿá@Exif��II*����������������¬������r������ z��(�������� ��������������2�������� ������ ��� ��1�(����©������������$�� ������0220� ����0100 ������ ������ ���������������� ����X���������������������������� ��"�� ����'���� ������ ���������������������������������� ����!��¢��������£������£��������������������¤�������� ¤��������¤����)��¤ ������¤������������������������������ ¤�������� ¤������������������������1������9��(������������A�� ����÷������H����� �H������2014:02:28 11:05:03�Panasonic�DMC-FX01�PaintShop Pro 15,00�������������������� ������2013 :06:20 15:42:46�2013:06:20 15:42:46�

它不记录任何东西。您知道如何访问已发送文件的文件名和文件本身的内容吗?

感谢您的分析。

【问题讨论】:

  • 你应该调用你的fileUpload方法fileuploadAction,它应该在一个控制器中,你应该为它定义一个路由。在项目主目录的控制台中使用 app/console router:debug 来查看所有有效路由。
  • 好的,但是如何访问 $_POST 数组来获取文件?
  • 想知道formData如何照顾.append(...)
  • 我从未见过 ajax 上传文件。只有具有多部分请求的普通表单。如果有办法我很好奇。让我知道你发现了什么。
  • 我设法通过 ajax 请求做到了这一点。效果很好。通过ajax发送FormData对象后文件可以通过move_uploaded_file($request->files->get(filenumber),path);

标签: php ajax symfony multipartform-data


【解决方案1】:

嗯.. 在普通 PHP 中,文件存储在临时目录中,文件信息通过 $_FILES 数组提供。

在 Symfony 中,我们不希望直接使用超全局数组,但我们可以例如使用 Request object。用于文件上传you should read this

【讨论】:

    【解决方案2】:

    你的问题

    在你的 JS 中

    formData.append('photos[]', file, file.name);
    

    这意味着 symfony 将把它作为 photos 放在一个数组中。

    $uploads = $request->files->get('photos');
    

    $uploads 是一个文件对象数组。您将需要循环 $uploads 并处理每个文件。


    旁注

    如果您不是在一个请求中上传多个并且只想要一个上传,而不是作为一个数组,那么将您的 JS 更改为:

    formData.append('photo', file, file.name);
    

    然后在 Symfony 中你会使用

    $upload = $request->files->get('photo');
    

    $upload 是一个文件引用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-14
      • 2021-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      相关资源
      最近更新 更多