Jump to content

Problems with XMLHttpRequest Level 2

- - - - -

  • Please log in to reply
11 replies to this topic

#1
Topacio86

Topacio86

    Newbie

  • Members
  • Pip
  • 2 posts
Hello!
I'm trying to write service, what will upload files through XMLHttpRequest Level 2, but I have problems with big files (more than 60Mb). I receive this message (for 65 Mb)

Fatal error</b>: Allowed memory size of 209715200 bytes exhausted (tried to allocate 71899100 bytes)

Size of memory is 200 Mb and I can't guarantee that value on hosting will greater or the same at least. I don't understand, what to do to solve this problem. My code is here:

<form id="myform" name="myform">

  <input type="file" name="failik" id="failik" onchange="sendFile(this.form);">

</form>


<div id="uploadProgress"></div>

<script type="text/javascript">

<!--

  function sendFile(form) {

    var files = document.getElementById("failik").files;

    

    for (var i = 0; i < files.length; i ++) {

      file = files[i];

      var name = file.fileName != null ? file.fileName : file.name;

      var size = file.fileSize != null ? file.fileSize : file.size;

                     

      var xhr = new XMLHttpRequest();

                           

      xhr.upload.onprogress = function(e){

          if (e.lengthComputable){

              var value = Math.round(e.loaded / e.total * 100);

              var text = value + '% from ' + e.total;

              document.getElementById("uploadProgress").innerHTML = text;

          }

      };

      

      xhr.onreadystatechange = function(){            

          if (xhr.readyState == 4) {

              alert(xhr.responseText);                   

          }

      };

      

       xhr.open("POST", "/do-nothing.php?file=" + name, true);

       xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");

       xhr.setRequestHeader("X-File-Name", encodeURIComponent(name));

       xhr.setRequestHeader("Content-Type", "application/octet-stream");

       //I've tried to do this

       var formData = new FormData();

       formData.append(name, file);

       xhr.send(formData);

      //And this

      var formData = new FormData(form);

      xhr.send(formData);

      //And this

      xhr.send(file);

      //But without result

    }

  }

//-->

</script>



#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Maybe try uploading it in multiple chunks and having a server-side script put it back together?
sudo rm -rf /

#3
Topacio86

Topacio86

    Newbie

  • Members
  • Pip
  • 2 posts
I did this and now think about server script

#4
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Use a PHP session to keep track of what file is being uploaded, then just reopen the file for appending on every POST request and dump the data in.
sudo rm -rf /

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

Quote

var xhr = new XMLHttpRequest();

This might not be the problem, but that code probably won't work on IE.

Why not use something like this?:
var xhr; 

if (navigator.appName == "Microsoft Internet Explorer") xhr= new ActiveXObject("Microsoft.XMLHTTP"); 

else xhr= new XMLHttpRequest(); 


#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
if( window.XMLHttpRequest )[COLOR=#000000]
    xhr = XMLHttpRequest();
else
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
[/COLOR]

sudo rm -rf /

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US

Quote

if( window.XMLHttpRequest )

    xhr = XMLHttpRequest();

else

    xhr = new ActiveXObject("Microsoft.XMLHTTP");

Yeah, but I tried that before and it gave me an error in IE.
Webpage error details


User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E)

Timestamp: Wed, 3 Aug 2011 06:36:01 UTC



Message: Invalid procedure call or argument

Line: 9

Char: 5

Code: 0

URI: http://192.168.0.2/ajax-1.html



The HTML:
<html> 

	<head> 

		<title> AJAX Test 01 </title> 

	</head> 

	<body> 

		<h1> AJAX Test 01 </h1> 

		<script type="text/javascript"> 

			if( window.XMLHttpRequest )

				xhr = XMLHttpRequest();

			else

				xhr = new ActiveXObject("Microsoft.XMLHTTP");

			xhr.open("GET", location, false); 

			xhr.send(); 

			document.write(xhr.responseText); 

		</script> 

	</body> 

</html> 

But it does work when I use the "if (navigator.appName ..." method.



EDIT: I tried inserting "var xhr; " right before the 'if' statement, and it still gave me that error.

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Weird...works for me, unless I'm doing something different. What version are you using?
sudo rm -rf /

#9
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Here's the help message box:Attached File  ie-ver.png   26.55K   13 downloads

#10
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,722 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I'm not booted into my Windows partition right now; I'll check later and post back. This is weird.
sudo rm -rf /

#11
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
You're missing the "new" keyword:
[FONT=monospace]xhr = [COLOR=#ff8c00][FONT=arial black][B]new[/B][/FONT][/COLOR] XMLHttpRequest();[/FONT]


#12
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Mozilla Firefox, Google Chrome, and Opera don't seem to complain about the fact that the 'new' keyword is missing. It looks like Internet Explorer and Apple Safari require that keyword.

It works now.



And the other thing, if not using asynchronous requests, this doesn't work in Internet Explorer, for some reason:
xhr.open("GET", "/somefile", 0) 
But this should work:
xhr.open("GET", "/somefile", false) 
That was another cause of AJAX to fail in scripts I written before.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users