国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

javascript - Why can't I upload images without refreshing in php + ajax?
大家講道理
大家講道理 2017-05-24 11:30:56
0
2
628

Write a relatively low file upload function. You need to click Upload to upload it to the server.
But after testing, it still fails to upload. I don’t know what’s going on? Below is my code:

html

<body>
    <p class="toolbar1">
        <a href="javascript:;" id="upload" class="a-upload mr10"><input id="changeFile" type="file" name="myFiles" id="">點擊這里上傳文件</a>
        <p class="showFileName mr10"></p>
        <button id="uploadBtn" type="button" class="btn btn-primary">上傳</button>
    </p>
</body>

php:

<?php 
    //做個路由 action為url中的參數(shù)
    $action = $_GET['action'];
    switch($action) {
        case 'upload_file':
            upload();
            break;
    }
    
    function upload(){
        // 獲取上傳的圖片
        $pic = $_FILES['myFiles']['tmp_name'];
        $upload_ret = false;
    
        if($pic){
            // 上傳的路徑,建議寫物理路徑
           $uploadDir = "../upload_files";
            // 創(chuàng)建文件夾  
            if(!file_exists($uploadDir)){        
                mkdir($uploadDir, 0777);    
            }    
            // 用時間戳來保存圖片,防止重復
            $targetFile = $uploadDir . '/' . time() . $_FILES['myFiles']['name'];    
            // 將臨時文件 移動到我們指定的路徑,返回上傳結果
            $upload_ret = move_uploaded_file($pic, $targetFile) ? true : false;
        }
    
        return $upload_ret;
    }
?>

js:

$(function() {
    //上傳圖片
    $('#uploadBtn').on('click',function(){
        /*alert('444');*/
        uploadFiles();
    })

    function uploadFiles(){
        //上傳文件接口
        var uploadUrl = "./php/data.php?action=upload_file";
        //選擇的文件
        var files = document.getElementById('changeFile').files[0];
        var fileArr = [files];
        //經過調試是不進ajax的
        $.ajax({
            type:"post",
            url:uploadUrl,
            data:fileArr,
            dataType: 'json',
            contentType: 'application/json;charset=utf-8',
            success: function(data) {
                console.log(data);
            },
            error: function(data){
                        
            }
        });
    }
}

After debugging, ajax is not entered in js. I don’t know what is the problem? Please help, thank you!
In php, $pic = $_FILES['myFiles']['tmp_name']; How to get the value of $pic? During debugging, I found that this value cannot be obtained. In js, I convert the obtained file information into an array and pass it through ajax post. How do I pass this array to php?

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

reply all(2)
漂亮男人

You are passing mixed data, try changing contenttype and processData to false

$.ajax({
            type:"post",
            url:uploadUrl,
            data:{ "yourfiles": files} //這里改成obj對象,
            dataType: 'json',
            contentType: false,
            processData:false,
            success: function(data) {
                console.log(data);
            },
            error: function(data){
                        
            }
        });
迷茫

processData should be set to false

You need to use formData to submit using Ajax

var file = document.getElementById('changeFile').files[0]
var uploadUrl = "./php/data.php?action=upload_file"
var fd = new FormData()

fd.append('myFiles', file)

$.ajax({
    type:"post",
    url:uploadUrl,
    data: fd,
    processData: false,
    contentType: false,
    success: function(data) {
        console.log(data);
    },
    error: function(data){
        
    }
})
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template