試試這個:
$(document).ready(function(){
$('#upload').on('click', function() {
var file_data = $('#pic').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
$.ajax({
url : 'upload.php', // point to server-side PHP script
dataType : 'text', // what to expect back from the PHP script, if anything
cache : false,
contentType : false,
processData : false,
data : form_data,
type : 'post',
success : function(output){
alert(output); // display response from the PHP script, if any
}
});
$('#pic').val(''); /* Clear the file container */
});
});
</script>
</head>
<body>
<input id="pic" type="file" name="pic" />
<button id="upload">Upload</button>
</body>
upload.php的
<?php
if ($_FILES['file']['error'] > 0){
echo 'Error: ' . $_FILES['file']['error'] . '<br>';
}
else {
if(move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']))
{
echo "File Uploaded Successfully";
}
}
?>
var文件= $( 「#CB」)[0] .files [0]。名稱;點擊按鈕點擊 –
是的,那是不可能的? –
你想得到什麼? –