2012-12-05 73 views
0

用c#我下載文件與webclient作爲字符串以檢查下載速度,我怎麼能用javascript或PHP做到這一點? 這是我的c#示例。如何使用javascript或php下載文件爲字符串作爲speedtest?

Uri URL = new Uri("https://www.example.com/File512kb"); 
    WebClient wc = new WebClient(); 
    double starttime = Environment.TickCount; 
    string file = wc.DownloadString(URL);  //download this file as string so I won't need to save it to local disk. 
    stopWatch.Elapsed.Milliseconds; 
    double endtime = Environment.TickCount; 
    double milisecs = endtime - starttime; 

謝謝...

+0

[你有什麼嘗試?](http://www.whathaveyoutried.com/)你可以發佈一些代碼? – Cerbrus

回答

1

的JavaScript :

var start = new Date(); 
$.ajax({ 
    url: "https://www.example.com/File512kb" 
}).done(function() { 
    console.log(new Date() - start); 
}); 

這將返回以毫秒爲單位的時間。然而,該函數確實使用jQuery,因爲這是一種很簡單的AJAX調用方式。

1

它不會是100%準確,但你可以在PHP中使用file_get_contents()microtime()

http://us3.php.net/file_get_contents http://us1.php.net/manual/en/function.microtime.php

1
$fStart = microtime(true); 

file_get_contents('http://www.example.com/File512kb'); 

$fEnd = microtime(true); 

echo $fEnd - $fStart; 
1

這是我怎麼會去在PHP:

<?php 

    set_time_limit(0); 
    $start_time = microtime(true); 
    $file = file_get_contents('http://somefileorother.com/file'); 
    $end_time = microtime(true) - $start_time; 

    echo $end_time; 
相關問題