weixin_33688840 2016-05-15 20:40 采纳率: 0%
浏览 615

JS-使用Ajax获取文件名

I am trying to get the extension and name of a file using Ajax.

My ajax code looks like:

$.ajax({
    type: "POST",
    url: url,
    data: $("#updateMember").serialize(), 
    mimeType: "multipart/form-data",
    contentType: false,
    cache: false,
    processData: false,
    success: function (data) {
        alert(data);
    }
});

In PHP I have this

$imageFileType = pathinfo($_FILES["input9"]["name"], PATHINFO_EXTENSION);
echo $imageFileType;

and my html is

<form method="post" enctype="multipart/form-data" id="updateMember">
    <input id="input-9" name="input9" type="file" class="file file-loading" data-allowed-file-extensions='["png", "jpg", "gif", "jpeg"]' style="top: 0px;">
    <li class="menu__item"><input name="offerUpload" id="dashOffersBtn" class="menu__link menu_input btnDashboard" style="margin: 0 auto; height:20px;padding: 0;padding-bottom: 22px;width: 70%;" type="button" value="SAVE"></li>
</form>

but it seems to not work. What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • bug^君 2016-05-15 20:59
    关注

    You have to use FormData() to make this possible, serialize or serializeArray() does not support file api's use the below solution :

    Main update for working solution

    I did update the Save button's type from button to submit please use the below html :

    <form method="post" enctype="multipart/form-data" id="updateMember">
        <input id="input-9" name="input9" type="file" class="file file-loading" data-allowed-file-extensions='["png", "jpg", "gif", "jpeg"]' style="top: 0px;">
        <li class="menu__item">
        <input name="offerUpload" id="dashOffersBtn" class="menu__link menu_input btnDashboard" style="margin: 0 auto; height:20px;padding: 0;padding-bottom: 22px;width: 70%;" type="submit" value="SAVE">
        </li>
    </form>
    

    and use the below jquery code to make it work

    jQuery('#updateMember').submit(function(e){
               e.preventDefault();
                var data = new FormData(this);
                    $.ajax({
                        type: "POST",
                        url: 'upload.php',
                        data: data, 
                        mimeType: "multipart/form-data",
                        contentType: false,
                        cache: false,
                        dataType: "html",
                        processData: false,
                        success: function(data) {
                            alert(data);
                        }
                    });
            });
    

    also use the below php code for extension :

    $data = $_FILES['input9'];
    $data_ext = explode('/',$data['type']);
    $extension = $data_ext[1];
    echo $extension;
    

    My test index.php code

    http://pastebin.com/L81xUbxL

    Change Url To your target url.

    Old non working

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

    for getting an extension you can do this

    $path_ = pathinfo($_FILES["input9"]["name"]);
    $extension = $path_['extension'];
    echo $extension;
    
    评论

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大