为什么上传PDF文件时Ajax无法正常工作?

时间:2013-12-28 01:28:25

标签: php jquery ajax pdf file-upload

我一直在使用Ajax发送和上传文件,一切都很完美,我可以上传照片和文件没有问题,但现在它只是不适用于.pdf文件。

这就是我从jQuery文件获得的原因:

function uploadImage(){
    $('#file').change(function(){  
        var file = this.files[0]
        if(file){
            $('.alert.alert-danger').hide();
            $('#filename').text(file.name);
            $('#filename').show();
            $('.cargando').show();
            $('.uploaded').hide();

            $('#file').hide();
            name = file.name;
            size = file.size;
            type = file.type;
            uploadAjax();
        }

    });
    $('#uploadremove').click(function(e){
        e.preventDefault();
        $('.alert.alert-danger').hide();
        $('#file').show();
        $('.cargando').hide();
        $('#filename').hide();
        $('#uploadok').hide();
        $('#uploadremove').hide();
        $('#file_id').val("0");
        $('.fileinput-button').show();
        $('.savebtn').toggleClass('disabled');
        $('.uploaded').hide();
    });
}

function uploadAjax(){ 
    var inputFileImage = document.getElementById("file"); 
    var filelocation = $('#file').val(); 
    var file = inputFileImage.files[0]; 
    var info = new FormData(); 
    var tipo_archivo = $("#file").attr('accept');
    var tipo_archivo = type;
    info.append('tipo', tipo_archivo); console.log(file);
    var url = HTTP+"admin/uploadfile";
    $.ajax({
        url:url,
        type:'POST',
        contentType:false,
        data: info,//{archivo : file, tipo : tipo_archivo},
        processData:false,
        cache:false,
        success:function(response){
            if(response != '' && !($.isNumeric(response)) ){
                $('.alert.alert-danger #uploaderror').html(response);
                $('.alert.alert-danger').fadeIn(2000);
                $('.cargando').hide();
                $('#filename').hide();
                $('#file').show();
            }
            else{
                $('.cargando').hide();
                $('#uploadok').show();
                $('#uploadremove').show();
                $('#file_id').val(response);
                $('.fileinput-button').hide();
                $('.savebtn').toggleClass('disabled');
                if(tipo_archivo == "image/*"){
                    console.log(file);
                }
            }
        }
    });
}

这是HTML表单:

<form class="form-horizontal validate" role="form" method="post" action="<?=fk_link().'admin/savecv'?>">
    <input name="location" type="hidden" class="location" value="admin/addcv/<?=@$profesor['id']?>" >
    <input name="profesor_id" type="hidden" value="<?=@$profesor['id']?>" >
    <div class="form_message">
        <div class="alert alert-warning">
        </div>
    </div>
    <? 
    $texto = "Seleccionar Archivo PDF";
    if(@$profesor['profesor_cv']){ $texto = "Cambiar Cv";?>
    <div class="uploaded">
        <a class="btn btn-success fileDownloadPromise" target="_blank" href="<?=fk_link().'uploads/'.@$cv['cv']?>">
            <i class="glyphicon glyphicon-cloud-download"></i>
            <span>Descargar</span>
        </a>
    </div>
    <? 
    } else $texto = "Seleccionar Archivo PDF";?>
    <span class="btn btn-danger fileinput-button">
        <i class="glyphicon glyphicon-cloud-upload"></i>
        <span><?=$texto?></span>
        <!-- The file input field used as target for the file upload widget -->
        <input id="file"  type="file" name="fileToUpload" id="filesToUpload">
    </span>
    <span class="cargando"><img src="<?=fk_theme_url().'/images/loading.gif'?>"></span>
    <span id="filename"></span>
    <input id="file_id" name="file_id" type="hidden" value="0" >
    <span id="uploadok" class="glyphicon glyphicon-ok"></span>
    <a href="#" id="uploadremove">Cambiar</a>

    <div class="clearfix" style="height:10px"></div>
    <div class="alert alert-danger col-md-3" style="display:none;">
        <span class="glyphicon glyphicon-warning-sign"></span>
        <span id="uploaderror">error example</span>
    </div>
    <input class="btn btn-primary savebtn disabled" value="Guardar" type="submit">
</form>

正如我所说,一切正常,但现在只适用于图像。

1 个答案:

答案 0 :(得分:-1)

info.append('tipo', tipo_archivo);

应该有:

info.append('tipo', file);