将文件img上传到forder

时间:2017-04-20 09:43:23

标签: php ajax file-upload asyncfileupload

有人可以帮助我如何使用ajax将图像文件上传到文件 这是我的形式:

<form class="form-create" method="post">
     <input type="file" name="file" size="20" />
     <input type="button" name="submit" onclick="addproduct()" value="upload">
</form>

1 个答案:

答案 0 :(得分:0)

Ajax调用

$.ajax({
    url: "ajax_php_file.php", // Url to which the request is send
    type: "POST",             // Type of request to be send, called as method
    data: new FormData(this), // Data sent to server, a set of key/value pairs (i.e. form fields and values)
    contentType: false,       // The content type used when sending data to the server.
    cache: false,             // To unable request pages to be cached
    processData:false,        // To send DOMDocument or non processed data file it is set to false
    success: function(data)   // A function to be called if request succeeds
    {
    $('#loading').hide();
    $("#message").html(data);
    }
    });

php函数

$sourcePath = $_FILES['file']['tmp_name'];       // Storing source path of the file in a variable
$targetPath = "upload/".$_FILES['file']['name']; // Target path where file is to be stored
move_uploaded_file($sourcePath,$targetPath) ;    // Moving Uploaded file