The Simplest Version Of Ajax (jquery) To Upload Just One File
I just want user to select a file and it would automatically download it to my server. I don't need no more features. What is the simplest and most reliabe (maybe you used it?) plu
Solution 1:
from a web page such as: upload.html
<html><head><scripttype="text/javascript">functionuploadFile(){
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=newXMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=newActiveXObject("Microsoft.XMLHTTP");
}
var formElement = document.getElementById("upload");
var formData= newFormData(formElement);
xmlhttp.open("post","upload.php",false);
xmlhttp.send(formData);
var counter = 0;
while (xmlhttp.readyState != 4){
counter = counter + 1;
}
var errorCondition = xmlhttp.responseText;
if(errorCondition == "success"){
alert("File uploaded successfully");
}
else{
alert("Error: "+errorCondition);
}
}
</script></head><body><formid="upload"action="upload_file.php"method="post"enctype="multipart/form-data"><labelfor="file">Filename:</label><inputtype="file"name="file"id="file" /><br /><inputtype="button"name="submit"value="Submit"onclick="uploadFile();" /></form></body></html>
that calls php such as this: upload..php
<?phpif ($_FILES["file"]["error"] > 0)
{
echo ($_FILES["file"]["error"]);
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo$_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo"success";
}
}
?>
Post a Comment for "The Simplest Version Of Ajax (jquery) To Upload Just One File"