duanaiguang1960 2017-04-24 00:52
浏览 129
已采纳

使用ajax上传文件

I´ve been looking at all the answers i could find in here, but none seems to work for me, not sure why.

I´m trying to upload a file using ajax, but the file never gets to the $_FILES array in PHP, i only makes it to the $_POST array but it won´t let me get the name or size of the file. I've tried every single solution answered here but none seems to solve my problem and i'm not sure why, i think there is something i'm not seeing. If anyone can help me that would be awesome.

THE HTML CODE

<form name='formuploadgraphic'>
     <input type='file' name='archivo1' id='archivo1' onchange='subegraphic()'>
</form>

THE JS/AJAX CODE

function subegraphic()
{
    var formData = new FormData();
    formData.append("archivo1", $("#formuploadgraphic")[0]);

    $.ajax({
        url: "subegraphic.php",
        type: "post",
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        async: true
    })
     .done(
         function(data)
         {
             alert(data);
         }
     );
}

THE PHP CODE

<?php
    $upload_folder ='graphics';
    $nombre_archivo = $_FILES['archivo1']['name'];
    $tipo_archivo = $_FILES['archivo1']['type'];
    $tamano_archivo = $_FILES['archivo1']['size'];
    $tmp_archivo = $_FILES['archivo1']['tmp_name'];
    $extension = pathinfo($_FILES['archivo1']['name'], PATHINFO_EXTENSION);
    $archivador = $upload_folder . '/' . $nombre_archivo;
    echo $nombre_archivo;
?>

This is the reply from the php if i use that code:

"
Notice: Undefined index: archivo1 in C:\xampp\htdocs\airpost\home\subegraphic.php on line 4

Notice: Undefined index: archivo1 in C:\xampp\htdocs\airpost\home\subegraphic.php on line 5

Notice: Undefined index: archivo1 in C:\xampp\htdocs\airpost\home\subegraphic.php on line 6

Notice: Undefined index: archivo1 in C:\xampp\htdocs\airpost\home\subegraphic.php on line 7

Notice: Undefined index: archivo1 in C:\xampp\htdocs\airpost\home\subegraphic.php on line 8
"

If anyone can tell what is the problem with my code that would be grate. Thx in advance.

PS: upload files is ON in the php.ini

  • 写回答

2条回答 默认 最新

  • doudouwd2017 2017-04-24 01:39
    关注

    From the Code Snippet you posted, your jQuery selector is pointing to an undefined element

    Change

    formData.append("archivo1", $("#formuploadgraphic")[0]);
    

    to

    formData.append("archivo1", $("#archivo1").prop('files')[0]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向