Go Back   CodingForums.com > :: Client side development > JavaScript programming > Ajax and Design

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-04-2011, 10:17 PM   PM User | #1
Sandeep2010
New Coder

 
Join Date: Nov 2010
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Sandeep2010 has a little shameless behaviour in the past
Question Using Ajax To Upload files!!Need Help

Please help me with Ajax file upload.Is it possible to upload a file to the server using ajax with this method? I am using the following piece of code:
Code:
<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('ajaxDiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		        
                }
                
	}
  
        
	ajaxRequest.open("POST","upload.php", true);
ajaxRequest.setRequestHeader("Content-type","application/x-www-     form-urlencoded");	
ajaxRequest.send(); 
}

//-->
</script>
What should be inside send() within :ajaxRequest.send()???
The input field name for file is file[]. I am uploading multiple files.
upload.php contains code to upload files.When I am using the script without ajax(by using action="upload.php" directly in form) it works.But when I use ajax it does not work.
Please help me.
Thanks! :-)
Sandeep2010 is offline   Reply With Quote
Old 01-05-2011, 06:50 AM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
No, unfortunately file upload is not part of ajax functionality for security reasons. All file uploads have to be started by a user with the usual browser button.
devnull69 is offline   Reply With Quote
Old 01-05-2011, 07:08 AM   PM User | #3
Sandeep2010
New Coder

 
Join Date: Nov 2010
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Sandeep2010 has a little shameless behaviour in the past
Quote:
Originally Posted by devnull69 View Post
No, unfortunately file upload is not part of ajax functionality for security reasons. All file uploads have to be started by a user with the usual browser button.
Thanks for the insight.But Can you please help accomplish file upload without refreshing the page?I have seen in many sites that they use ajax to accomplish this.Can you please suggest any idea?
Sandeep2010 is offline   Reply With Quote
Old 01-05-2011, 10:34 AM   PM User | #4
Sandeep2010
New Coder

 
Join Date: Nov 2010
Posts: 17
Thanks: 1
Thanked 0 Times in 0 Posts
Sandeep2010 has a little shameless behaviour in the past
Can Anyone Help me out please?
Sandeep2010 is offline   Reply With Quote
Old 01-05-2011, 11:01 AM   PM User | #5
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
Quote:
Originally Posted by Sandeep2010 View Post
I have seen in many sites that they use ajax to accomplish this
Most likely they used Flash, because it just isn't possible with Ajax.
devnull69 is offline   Reply With Quote
Reply

Bookmarks

Tags
ajax, file, php, script, upload

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:32 PM.


Advertisement
Log in to turn off these ads.