我想使用AJAX上傳輸入類型文件的onchange
中的圖像。我只能使用javascript,ajax和php。如何使用javascript,ajax和php上傳圖像
看看我的代碼:
的index.html
<form id="myForm" action="" enctype="multipart/form-data">
<input type="file" name="imagefile" id="imagefile" onchange="uploadImage()">
</form>
upoad.js
function uploadImage(){
try {
ajpass = new XMLHttpRequest();
} catch (e) {
ajpass = new ActiveXObject("Microsoft.XMLHTTP");
}
ajpass.onreadystatechange = epasscheck2;
ajpass.open("post", "http://localhost/moodle/lib/editor/tinymce/plugins/moodleimage/upload.php", true);
ajpass.send();
}
function epasscheck2() {
if ((ajpass.readyState == 4) && (ajpass.status == 200)) {
var restxt = ajpass.responseText;
alert(restxt);
}
}
upload.php的
<?php
echo $_FILES["imagefile"]["name"]; //error here
//file upload code here
?>
我在upload.php
中收到錯誤Undefined index imagefile
。
我無法將圖像文件屬性(如名稱,大小,tmp_name等)從upload.js
傳遞到upload.php
。
嘗試單維數組即如$ _FILES [「imagefile」] –
已經有很多帖子在SO上,仍然不知道? – Viney