WebFX WebBoard
Message Thread:
View All Messages
Back to WebFX
 xmlhttp request object file uploads - possible?, Peter Hawkes, February 12, 2004
         Re:xmlhttp request object file uploads - possible?, Tom Trenka, February 13, 2004
             Re:xmlhttp request object file uploads - possible?, Erik Arvidsson, February 18, 2004

Subject: xmlhttp request object file uploads - possible? From: Peter Hawkes Date: February 12, 2004

Hello Erik,

I use the xmlhttp request object extensively in my PHP web apps, I have done all the usual stuff such as poll my database (MySQL) for duplicate user names and alert back without page reload. I have also used it to load text/html into a page similar to what can be done with Flash remoting in a round about way.

My problem now is I am wanting to use the xmlhttp request to upload files, what I have is this:

var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
  try {
  xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
 } catch (e) {
  try {
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  } catch (E) {
   xmlhttp=false
  }
 }
@else
 xmlhttp=false
@end @*/
if (!xmlhttp) {
 try {
  xmlhttp = new XMLHttpRequest();
 } catch (e) {
  xmlhttp=false
 }
}
function xmlhttpUpload(url,obj) {
var obj = MM_findObj(obj);
if(xmlhttp){
 xmlhttp.open("POST",url, false);
 xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 // xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");

// what header should I use?
 xmlhttp.onreadystatechange=function() {
  if (xmlhttp.readyState==4) {
   alert(xmlhttp.responseText);
   obj.innerHTML = 'uploading';
   if(xmlhttp.responseText==""){
    obj.innerHTML = 'upload failed';
   } else {
    obj.innerHTML = xmlhttp.responseText;
   }
  }
 }
 
 xmlhttp.send("s="+document.forms[0].elements['imageUpload'].value);

// document.forms[0].elements['imageUpload'].value is my file field value
  } else {
 alert('Sorry your browser does not support the XML HTTP Request Object');
 }
}

Basically the problem is my JS above sends this to my PHP page but my PHP page is sending back a message that my file field is a $_POST var instead of a $_FILES var, therefore I can not process the upload with PHP because it sees it as a text field and not a file field.

Any tips would really be appreciated and although I *think* this can be done, I am not 100% sure so please advise.

First time I have posted here, I want to take this opportunity to thank you for a great site and resource.

Pete




Reply
Name: 
Email: 
Subject: 

Enter your reply to this message below. HTML tags are not supported but words that start with http://, ftp:// or mailto: are converted to links.


View All Messages
Back to WebFX