简体   繁体   English

jQuery-使用ajax的DataTables上传文件

[英]jQuery - DataTables upload file using ajax

I am using the plugin datatable to show a list of users. 我使用的插件, datatable显示的用户列表。 In this list I need to be able to upload new files. 在此列表中,我需要能够上传新文件。

I am trying to use ajax to try to send data to php. 我正在尝试使用Ajax尝试将数据发送到php。 If I send value there is not problem my php script works however, I cannot get the data from the files. 如果我发送值没有问题,但是我的php脚本有效,但是我无法从文件中获取数据。

In order to upload the files I am using the same script that I have written for another project which is working fine, so I think here the problem is DataTable that do not recognise my form data. 为了上载文件,我使用的是我为另一个可以正常运行的项目编写的脚本,因此我认为这里的问题是DataTable无法识别我的表单数据。

Anyone knows how to achieve this? 有人知道如何实现这一目标吗?

FIDDLE 小提琴

JS JS

$('#example .fileinput-upload-button').on('click', function(event) {

  var td = $(this).closest("td");
  var parentTD = td.parent();

  var form = $(this).closest("form");
var url = "example/upload.php?type=photo"
  var data = new FormData(form);
  alert(form);

  $.ajax({
    type: "POST",
    url: url,
    data: data,
    mimeType: "multipart/form-data",
    contentType: false,
    cache: false,
    dataType: "html",
    processData: false,
    success: function(data) {

      alert(data);
    }
  });

PHP 的PHP

$type = filter_input(INPUT_GET, "type");


$target_dir_header = $includesDir . "dashboard/resources/header_pic/";
$dataHeader = $_FILES['input7'];
$dataHeader_ext = explode('/', $dataHeader['type']);
$imageFileType_header = $dataHeader_ext[1];
$target_file_header = $target_dir_header . basename("header" . $imageFileType_header);


echo $type . " - " . $imageFileType_header;

Change your JQuery code to: 将您的JQuery代码更改为:

var td = $(this).closest("td");
var parentTD = td.parent();

var url = "example/upload.php?type=photo"
var form = $(this).closest("form").get(0);

   $.ajax({
   type: "POST",
   url: url,
   data: new FormData(form),
   mimeType: "multipart/form-data",
   contentType: false,
   cache: false,
   dataType: "html",
   processData: false,
   success: function(data) {
     alert(data);
   }

});

Your question explains here 您的问题在这里说明

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

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