0

I am really happy with the priceless suggestions that you all gave me last time.i really thank you all. But this time i am trying to upload an file using ajax. As the user select the file in the browse window, it straight away uploads the image. Please help me with this too. Thank you all.

This is my code:

function upd(str)
{
$.ajax({
       type:'post',
       url:'upld.php',
       datatype:'php',
       data:'fname='+str,
       success:function(responce)
       {
            alert("Uploaded");   
       }
       });
}

</script>
</head>

<body>
<form action="upd.php" method="post" enctype="multipart/form-data">

    <input type="file" name="namefile" id="namefile" onSelect="upd(this.value)">

</form> 

and my upld.php contains the following code:

<?php


$target="uploads/";
$target=$target.basename($_FILES['file']["name"]);
move_uploaded_file($_FILES['file']["tmp_name"],$target);
echo basename($_FILES["file"]["name"])."File Uploaded";


?>
2
  • This has been asked a million times. Here, have a look at the FormData API. mdn.beonex.com/en/DOM/XMLHttpRequest/FormData/… Commented May 28, 2014 at 10:42
  • Uploading files through ajax is not possible, but you can use base64 encoding and send a nice and long string to your backend :) Anyway, you can use systems like uploadify (uses flash) to do it Commented May 28, 2014 at 10:42

1 Answer 1

2

You can use: $("form:first").submit();

On Change event can be attached against file object through jquery.

Otherwise, it may not be straight forward as you want. However, I have used Uploadify Jquery plugin that helps to do the following:

  • Upload multiple documents
  • Upload through drag and drop.

http://www.uploadify.com/about/

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks alot mate, i will look at the link you forwaded.
is there no way to modity te above code and upload the file.
instead of onselect, onchange should do the job for you. You can submit the form itself and you don't need ajax call here: <form action="upload.php" method="post"> <input type="file" onchange="this.form.submit()" name="myFile"/> </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.