douchuo9476 2013-06-29 14:58
浏览 26
已采纳

无目标的Ajax上传无法正常工作

I'm currently writing a website, which does upload files to a server. I'm trying to submit them via Ajax. Somehow that doesn't work and I don't know how to solve this.

This is what the form looks like

The HTML

Date of the Show

  </p>
  <p>
      Which Show<br/>
    <input type="text" class="span8" id="show" /></p>
  <p>
    Name of the Presenter<br/>
    <input type="text" class="span8" id="presenter" /></p>
  <p>

    Program Description <br/>
    <input type="text" class="span8" id="description" /></p>
<p>
  Upload your file <br />
  <input type="file" id="file" name="files" />
</p>
  <p>
    <input type="submit" name="submit" value="Upload your data" />
  </p>

The JavaScript/jQuery

    $('input[type="submit"]').click(function(event){
      var _date = $('#date').val();
      var _show = $('#show').val();
      var _presenter = $('#presenter').val();
      var _desc = $('#description').val();
      var _file = $('#file').val();

      event.preventDefault();

      $.post('ajax/submit.php',{"date": _date, 
                                    "show": _show,
                                    "presenter": _presenter,
                                    "description": _desc,
                                    "files": _file},
                                    function(data){
                                      console.log(data);

      })
      return false;
    })

The PHP

<?php
if(isset($_POST)){

    // Post variables
    $date       = $_POST['date'];
    $show       = $_POST['show'];
    $presenter  = $_POST['presenter'];
    $desc       = $_POST['description'];
    $file       = $_FILES["files"]; 

    // config for file handling (where to put)
    $upload_dir = "/opt/files/mdb/";
    $upload = $upload_dir.basename($_FILES['files']);

    // only allowing the filetpyes within the array
    $allowed_filtypes  = array('audio/mpeg');

    // Check if there is any error with uploading the file
    if($_FILES["files"]["error"] > 0){
        throw new exception('Error encountered');
    }else{
        if(in_array($_FILES['files']['type'],$allowed_filtypes)){
            move_uploaded_file($file, $upload);
        }else{
            print_r($_FILES);
            print_r($_POST);

        }   
    }
}else{
    echo "Wrong form key";
    // throw new exception('Wrong form key');

}
?>

When I print out the Arrays via print_r() the POST Array is full of my data but my FILES array is empty.

I think there is somehow a problem with the jQuery but so far I couldn't find anything useful on this.

Any help appreciated

  • 写回答

1条回答 默认 最新

  • douchao1957 2013-06-29 15:12
    关注

    The problem is you cannot do file upload through ajax unless you are using FormData https://developer.mozilla.org/en/DOM/XMLHttpRequest/FormData/Using_FormData_Objects This is supported in most browsers, but not in IE 9 or less

    You could use a flash based uploader like uploadify http://www.uploadify.com/

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝