Ajax上传不起作用 - 上传pdf文件时

时间:2012-08-31 10:50:32

标签: php ajax-upload

这是我的ajax代码:

$(function(){
    var cntUp = 0;
    var btnUpload=$('#upload_pdf');
    var status=$('#status');

    new AjaxUpload(btnUpload, {
        action: 'uplod_estimate_file.php',
        data: {saleid: $("#hid_saleid").val()},
        name: 'uploadfile_test',
        onSubmit: function(file, ext){
             if (! (ext && /^(jpg|pdf|jpeg)$/.test(ext))){ 
                // extension is not allowed 
                alert('Only JPG, PDF or JPEG files are allowed');
                return false;
            }
            this.setData({
                'saleid': $("#hid_saleid").val()
            });

            status.text('Uploading...');
        },
        onComplete: function(file, response){
            if (response.toLowerCase().indexOf("success") >= 0 ) {
                alert(response);
                /*
                var image='<img src="uploads/'+saleid+'/'+fname+'" alt=""  width="131px" height="125px"/>';
                $("#img0").html(image);*/

            }  else{
                $('<li></li>').appendTo('#files').text(file).addClass('error');
                //alert('error');
            }
        }
    });

});

这是表格:

<form name="frmvehdetails" id="frmvehdetails" method="post">

        <table cellpadding="3">
                  <tr>
            <td valign="top">VIN</td>
            <td>

                      <input class="frmIn" size="30"  name="car-vin" id="car-vin" type="text" value="" />
                      <input onclick="lookupVIN()" name="btnVin" type="button" value="Lookup" />
                      <br/>
                      <div id="vinDet" style="line-height: 20px; margin-bottom: 10px; margin-top: 10px;"></div></td>
          </tr>
                  <tr>
            <td>Year</td>
            <td><select name="car-years" id="car-years">
              </select></td>
          </tr>
                  <tr>
            <td>Make</td>
            <td><select name="car-makes" id="car-makes">
              </select></td>
          </tr>
                  <tr>
            <td>Model</td>
            <td><select name="car-models" id="car-models">
              </select></td>
          </tr>
          <tr>
            <td>Milage</td>
            <td><input type="text" name="car-milage" id="car-milage" value="" class="frmIn" /></td>
          </tr>

             <tr>
            <td>Repair Estimate</td>
            <td><input type="text" value="" id="car_estimate" name="car_estimate" class="frmIn"></td>
          </tr>
          <tr>
            <td>&nbsp;</td>
            <td><div id="upload_pdf" style="margin-bottom:10px;" >
                  <span>Upload File</span></div></td>
          </tr>


          <tr>
            <td>Reserve Price</td>
            <td><input type="text" value="" id="car_reserve" name="car_reserve" class="frmIn"></td>
          </tr>
                  <tr>
            <td>&nbsp;</td>
            <td>
                      <input type="button"  name="sub" value="<< Previous " class="previous-product" />
                     <input type="button" id="sub" name="sub" value="Next >>" class="next-product" onclick="validatevehicle();" /></td>
          </tr> <tr>
            <td>&nbsp;</td>
             <td id="v_errmsgs" style="color:red">
           </td>
          </tr>
                </table></form>
                <input type="hidden" id="hid_saleid" name="hid_saleid" value="" />

这是uplod_estimate_file.php:

<?php
session_start();
$sub = "";
$uploaddir = "";
if(isset($_POST['saleid'])){
    if($_POST['saleid']){
        $sub = $_POST['saleid'];
    }else{
        $sub = "";
    }
}else{
    $sub = "";
}
if($sub){
    $uploaddir = './uploads/'.$sub."/"; 
    }else{
        $uploaddir = './uploads/';
    }
$file = $uploaddir . basename($_FILES['uploadfile_test']['name']); 

if(move_uploaded_file($_FILES['uploadfile_test']['tmp_name'], $file)){
    echo "success";
}else{
    echo "error";
}


?>

但是当我使用这个时,我得到错误未定义索引'uploadfile_test'。我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:1)

如果它的大写扩展问题,如“PDF”,则更改:

onSubmit: function(file, ext){
   if (! (ext && /^(jpg|pdf|jpeg)$/.test(ext))){

onSubmit: function(file, ext){
   if (! (ext && /^(jpg|pdf|jpeg|pdf)$/i.test(ext))){ 

您可以将“ upload_max_filesize ”添加到.htaccess,例如:

php_value upload_max_filesize 32M