Click here to Skip to main content
15,889,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I want to test download speed for which I need to download a file of 100kb. This file is located on my server and I have written following code to download file using Ajax.

C#
xhr = new XMLHttpRequest();
   xhr.onreadystatechange=function()
   {
       if (xhr.readyState==4 && xhr.status==200)
       {
       alert("success");
       }
   }
 xhr.open("GET", "www.myserver.com/100kb.txt", false);
 xhr.responseType = "arraybuffer";
 xhr.send();


But I an unable to get the file. Do I need to write any .php script on server so that I can download this file in a background or I can get this file as payload.
On chrome and Mozilla it is not giving any error though I put my code in try and catch but on IE8.0 it gives permission denied.

Is there any XDomain issue.

Please suggest.

Regds
SNI
Posted
Comments
Sergey Alexandrovich Kryukov 14-Nov-13 2:33am    
If you are already using jQuery, why not using jQuery Ajax?
—SA

1 solution

yes you cannot do cross domain AJAX requests due to security reasons
you enable it from client side from the browser but that isnt really a solution
the preferred way is to make a php script that download the file and then use AJAX to present it to the client
 
Share this answer
 
Comments
SNI 19-Nov-13 6:44am    
Hi, the above code working fine when I write a php code at server side. I have setInterval to 2000ms. So after every 2000ms the request is sent to the server and data get downloaded in terms of http response but now my problem is since I am downloading a huge payload it is taking time (say 6-8 seconds) to load XMLHttp responses for concurrent requests. Can you pls advice what needs to be done to get faster faster response so that user can feel the data is downloading.
Manujith Pallewatte 20-Nov-13 1:05am    
heyy,
its your good time, only last week i made a php -> js library for just this purpose :)
https://github.com/ManZzup/pjstreamer
now this is actually not made for transfering byte data, but with some modifications you can make it transfer bytes over ajax
----------------
OR you can modify the solution to buffer data than downloading periodically, that is you tell the browser that the file is buffering and the browser will download byte stream till the server stops sending
SNI 20-Nov-13 5:09am    
Thanks. I opted the second option. Now I am able to get the heavy payload using php, which pushes the data to client but I need to wait till the data is completely downloaded to client. I tried to stop this outputting of buffer by clearing it at server side. But no success. Please let me know how to stop this kind of buffer caching at client side either in HTML or javascript after some time interval?
Manujith Pallewatte 20-Nov-13 5:47am    
you need to flush the buffer to break it, you need to tweak the browser to support this feature
i assume that the data you are transferring is okay with this breaking, following tip shows how to output php in realtime, in which the buffer is interrupted to show the output. Same concept can be used to tell the browser to stop receiving at a given time.
http://www.codeproject.com/Tips/680085/Real-time-Updating-of-PHP-Output

read more about http://php.net/manual/en/function.ob-flush.php (ob_flush()) as well

SNI 21-Nov-13 2:43am    
I tried with this but it doesn't work. I will explain what I am trying. In php I am reading a huge file (say 100MB) and I am echoing that. Pls see code below
$val=readfile("100mb.file");
ob_start ();
ob_flush();
echo $val;
sleep(10);
echo " <br />";
ob_flush();
ob_end_clean();
ob_end_flush();
I am clearing everything after 10 secs. After 10 secs I want to stop browser to stop this buffer downlaod/caching. But this does not work. Pls let me know where I am going wrong.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900