AjaxFileUpload插件不检索$ _POST数据 [英] AjaxFileUpload Plugin does not retrieve $_POST data

查看:126
本文介绍了AjaxFileUpload插件不检索$ _POST数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎是相同的问题,我面对了几天前。我修好了,但现在不再工作了。那么,它的一些工作。

我使用 AjaxFileUpload Plugin 来上传WP插件中的文件。这个插件调用 uploader.php 来处理上传表单。



我可以得到文件名数据)使用 $ _ FILES ['uploadFile'] ,但我无法检索 $ _ POST ['current_path']

我有一个理论。当我加载接口上传数据时,隐藏的输入字段'current_path'是空的(应该是)。当我浏览我的文件夹时,使用jQuery更新隐藏的输入字段。

当我点击上传按钮时,Ajax File Upload插件获取上传表单中的数据,并将数据传递给 uploader.php $ _ POST $ _ FILES



但为什么我可以从 $ _ FILES 而不是从 $ _ POST



以下是我的代码:

Javascript

  //文件上传函数
//删除上传的反馈信息click
jQuery('。uploadImage')。live('click',function(){
ajaxFileUpload ();
});
$ b(...)

//允许使用Ajax上传器插件上传文件
函数ajaxFileUpload(){
alert(jQuery('输入[type = hidden] [name = current_path]').val())//显示正确的当前路径
jQuery.ajaxFileUpload({
url:'../ wp-content / plugins /wp-filebrowser/uploader.php',
secureuri:false,
fileElementId:'uploadFile',
dataType:'json',
success:function(data){
if(data.error!=''){
alert(data.error);
} else {
alert(data.respons);
}

$ b $ {
jQuery('#uploadOutput')。addClass('error')。html('Error:'+ e).show();
$,
完成:function(){
//更新文件列表
}
}

return false;

$ / code>

HTML

 < form id =uploadFormenctype =multipart / form-dataaction =method =POST> 
< input type =hiddenid =current_pathname =current_pathvalue =<?php echo $ fb-> relative_url;?> />
< input id =uploadFilename =uploadFiletype =file/>
< input type =buttonclass =button uploadImagevalue =<?php _e('Upload File')?> /> < br />
< / form>

PHP

  $ this-> current_path = $ _POST ['current_path']; 
$ this-> data ['error'] = $ _FILES ['uploadFile'] ['name']; //仅用于测试
$ this-> data ['respons'] =文件名:$ _ POST ['current_path'];

echo json_encode($ this-> data);


解决方案


能够从$ _FILES获取数据,而不是从$ _POST获取数据?

因为您没有提交表单,只有文件输入元素。



这似乎是插件的行为 by design
$ b


在这个被黑客入侵的版本中,它只提交输入元素的指定文件类型而不是整个表单


jQuery表单插件可以做到这一点,也许这有助于。


This is almost identical problem which I faced a few days ago. I fixed it then, but now it's not working any more. Well, some of it works.

I'm using AjaxFileUpload Plugin to upload files in my WP plugin. This plugin calls uploader.php to process the upload form.

I am able to get the filename (and other data) using $_FILES['uploadFile'], but I'm not able to retrieve $_POST['current_path'] data.

I have a theory though. When I load the interface to upload data, the hidden input field 'current_path' is empty (as is hould be). As I navigate through my folders, the hidden input field is updated using jQuery.

When I hit the upload button, the Ajax File Upload plugin takes the data in the upload form and passes the data to uploader.php through $_POST and $_FILES.

But why am I able to get data from $_FILES and not from $_POST?

Here is my code:

Javascript

  //File upload functions
  // Remove feedback message on upload click 
    jQuery('.uploadImage').live('click',function() {
      ajaxFileUpload();
  });

  (...)

  //Lets upload the file by using Ajax uploader plugin
  function ajaxFileUpload() {
    alert(jQuery('input[type=hidden][name=current_path]').val()) //Shows me the correct current path   
    jQuery.ajaxFileUpload ( {
        url:'../wp-content/plugins/wp-filebrowser/uploader.php', 
        secureuri:false,
        fileElementId:'uploadFile',
        dataType: 'json',
        success: function (data) {
          if(data.error != '') {
              alert(data.error);
          } else {
              alert(data.respons);
          } 
        },
        error: function (e) {
            jQuery('#uploadOutput').addClass('error').html('Error: ' + e).show();
        },
        complete: function() {
          // Update file list
        }
      }
    )
    return false;   
  }

HTML

<form id="uploadForm" enctype="multipart/form-data" action="" method="POST">
    <input type="hidden" id="current_path" name="current_path" value="<?php echo $fb->relative_url; ?>" />
    <input id="uploadFile" name="uploadFile" type="file" />
    <input type="button" class="button uploadImage" value="<?php _e('Upload File') ?>" /> <br />
</form>

PHP

$this->current_path   = $_POST['current_path'];
$this->data['error']  = $_FILES['uploadFile']['name']; //Just for testing
$this->data['respons'] = "Filename: ".$_POST['current_path']; 

echo json_encode($this->data);  

解决方案

But why am I able to get data from $_FILES and not from $_POST?

Because you are not submitting the form, only the file input element.

It seems to be the plugin's behaviour by design:

In this hacked version, it submits the specified file type of input element only rather than an entire form

The jQuery form plugin can do both, maybe that helps.

这篇关于AjaxFileUpload插件不检索$ _POST数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆