dongtao9887
dongtao9887
2015-06-30 04:24

数据在jquery ajax中成功上载文件后无法打印成功消息

I am new to Jquery, Ajax and php, I written a program , which upload file to a database as a blob file , i succeeded in uploading the file to the database. but here comes a problem , now i am unable to print the success message in the <div id="msg"></div> section, after the file is being submitted. i tried the other ways and questions given in stackover flow but none worked out for me. the file "index.php"

contains code for ajax and file upload, where as the upload.php contains logic for inserting the file into database.

index.php

<?php include_once 'dbconfig.php'; ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>File upload</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $("#button").click(function() {
                var formData = new FormData();
                formData.append('file', $('#file')[0].files[0]);
                $.ajax({
                    url : 'upload.php',
                    type : 'POST',
                    data : formData,
                    processData: false,  
                    contentType: false, 
                    success : function(data) {
                        //console.log(data);
                        document.getElementbyID("msg").innerHTML()="";
                        alert(data);
                    }
                });
            });
        });
    </script>
</head>
<body>
    <div id="header">
        <tt>Upload file</tt>
    </div>
    <div id="msg"></div>
    <form id="form" name="form" enctype="multipart/form-data">
        <input type="file" name="file" id="file" />
        <button id="button" name="button"/>Submit</button>
    </form>
</body>
</html>

upload.php

<?php
    include_once 'dbconfig.php';

    $file = rand(1000, 100000)."-".$_FILES['file']['name'];
    $file_loc = $_FILES['file']['tmp_name'];
    $file_size = $_FILES['file']['size'];
    $file_type = $_FILES['file']['type'];
    $folder = "upload/";

    // new file size in KiloByte
    $new_size = $file_size / 1024;
    // make file name in lower case
    $new_file_name = strtolower($file);
    $final_file=str_replace(' ', '-', $new_file_name);

    if (move_uploaded_file($file_loc, $folder.$final_file)) {
        $sql = "INSERT INTO files(file) VALUES('$final_file')";
        mysqli_query($connection, $sql);
        mysqli_close($connection);
    }
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dreamfly0514 dreamfly0514 6年前

    You have to print response into php first. Add print "File uploaded successfully!!"; after mysqli_close($connection); line. JQuery code seems to be correct.

    <?php
    include_once 'dbconfig.php';
    
    try {
             $file = rand(1000,100000)."-".$_FILES['file']['name'];
             $file_loc = $_FILES['file']['tmp_name'];
             $file_size = $_FILES['file']['size'];
             $file_type = $_FILES['file']['type'];
             $folder="upload/";
    
             // new file size in KiloByte
             $new_size = $file_size/1024;
             // make file name in lower case
             $new_file_name = strtolower($file);
             $final_file=str_replace(' ','-',$new_file_name);
    
             if(move_uploaded_file($file_loc,$folder.$final_file))
             {
                  $sql="INSERT INTO files(file) VALUES('$final_file')";  
                  mysqli_query($connection,$sql);
                            
                  print "success";
             }
             else
            {
              print "error";
             }
      } catch ( Exception $e ) {
        print "error";
        }finally {
       mysqli_close($connection);  
      }
    ?>

    </div>
    
    点赞 评论 复制链接分享
  • dongshan0202405 dongshan0202405 6年前

    You can use a echo command in your php code and with echo you will be return a flag or message to understand your result of operation.

    In upload.php :

    <?php
     include_once 'dbconfig.php';
    
     try {
         $file = rand(1000,100000)."-".$_FILES['file']['name'];
         $file_loc = $_FILES['file']['tmp_name'];
         $file_size = $_FILES['file']['size'];
         $file_type = $_FILES['file']['type'];
         $folder="upload/";
    
         // new file size in KiloByte
         $new_size = $file_size/1024;
         // make file name in lower case
         $new_file_name = strtolower($file);
         $final_file=str_replace(' ','-',$new_file_name);
    
         if(move_uploaded_file($file_loc,$folder.$final_file))
         {
              $sql="INSERT INTO files(file) VALUES('$final_file')";  
              mysqli_query($connection,$sql);
    
              print "success";
         }
         else
        {
          print "error";
         }
    } catch ( Exception $e ) {
      print "error";
    }finally {
     mysqli_close($connection);  
    }
    ?>
    

    In index page :

                $.ajax({
                       url : 'upload.php',
                       type : 'POST',
                       data : formData,
                       processData: false,  
                       contentType: false, 
                       success : function(data) 
                           {
                            if(data ='success')                               
    
                              document.getElementbyID("msg").innerHTML()="success.";
    
                           }
                       });                                  
    
    点赞 评论 复制链接分享

相关推荐