2014-03-25 57 views
-2

我在c#应用程序中每隔几分钟就会用一个不同的数据更新一个文本文件。我如何刷新我的网站?

在文本文件中有例如第一次:你好世界 一分钟的文本文件后包含:大家

现在在C#应用程序即时上传文本文件,它曾经是变化的。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.IO; 
using System.Net; 

namespace ScrollLabelTest 
{ 
    class FtpFileUploader 
    { 
     static string ftpurl = "ftp://ftp.test.com/files/theme/"; 
     static string filename = @"c:\temp\test.txt"; 
     static string ftpusername = "un"; 
     static string ftppassword = "ps"; 
     static string value; 

     public static void test() 
     { 
      try 
      { 

       FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(
       ftpurl + "/" + Path.GetFileName(filename)); 
       request.Method = WebRequestMethods.Ftp.UploadFile; 


       request.Credentials = new NetworkCredential(ftpusername, ftppassword); 


       StreamReader sourceStream = new StreamReader(@"c:\temp\test.txt"); 
       byte[] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd()); 
       sourceStream.Close(); 
       request.ContentLength = fileContents.Length; 

       Stream requestStream = request.GetRequestStream(); 
       requestStream.Write(fileContents, 0, fileContents.Length); 
       requestStream.Close(); 

       FtpWebResponse response = (FtpWebResponse)request.GetResponse(); 

       Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription); 

       response.Close(); 
      } 
      catch(Exception err) 
      { 
       string t = err.ToString(); 
      } 
     } 
    } 
} 

我在我的硬盘上看到文本文件被改变的内容,也上传文件后我的网站ftp我看到那里更新的文本文件。

现在,在我的网站我使用的JavaScript/AJAX读取上传文本文件:

<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script src="http://newsxpressmedia.com/files/theme/jquery.newsTicker.js"></script> 

<div id="oneliner"> 
    <div class="header"> Breaking News </div> 
    <ul class="newsticker"> 
<script> 
$(function() { 
    var file = "http://newsxpressmedia.com/files/theme/test.txt"; 
    $.get(file, function (txt) { 
     //var lines = txt.responseText.split("\n"); 
     var lines = txt.split("\n"); 
     $ul = $('<ul class="newsticker" />'); 
     for (var i = 0, len = lines.length; i < len; i++) { 
      //save(lines[i]); // not sure what this does 
      $ul.append('<li>' + lines[i] + '</li>'); 
     } 
     //$ul.appendTo('body').newsTicker({ 
     $ul.appendTo('div.wcustomhtml').newsTicker({ 
      row_height: 48, 
      max_rows: 2, 
      speed: 6000, 
      direction: 'up', 
      duration: 1000, 
      autostart: 1, 
      pauseOnHover: 1 
     }); 
    }); 
}); 
</script> 
</ul> 
</div> 

的问题是,一旦我在C#应用程序更新文件,并将其上传到FTP我需要在我浏览器例如chrome手动刷新,如果没有,它将继续显示旧的文本文件内容,而不是更新。

如何在javascript中进行刷新?

+0

你是要自动刷新或手动(用户点击刷新)? –

+0

自动。例如,该网站将每分钟刷新一次。刷新或称之为更新。由于即时更新每分钟的文本文件,我希望网站每分钟读取更新的文件。它现在的方式是不断显示旧的文件内容。 –

+1

可能重复[如何重新加载页面每5秒?](http://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-second) – Shaharyar

回答

3

尝试这样

无需刷新页面只是刷新,将获得从文本文件

$(function() { 
    news(); 
    setInterval(function(){ 
     news() 
    },60000) // it will call every 1 min you can change it 
}); 

function news(){ 
    $('body').find('.newsticker').remove();//It will clear old data if its present 
    var file = "http://newsxpressmedia.com/files/theme/test.txt"; 
    $.get(file, function (txt) { 
     //var lines = txt.responseText.split("\n"); 
     var lines = txt.split("\n"); 
     $ul = $('<ul class="newsticker" />'); 
     for (var i = 0, len = lines.length; i < len; i++) { 
      //save(lines[i]); // not sure what this does 
      $ul.append('<li>' + lines[i] + '</li>'); //here 
     } 
     //$ul.appendTo('body').newsTicker({ 
     $ul.appendTo('div.wcustomhtml').newsTicker({ 
      row_height: 48, 
      max_rows: 2, 
      speed: 6000, 
      direction: 'up', 
      duration: 1000, 
      autostart: 1, 
      pauseOnHover: 1 
     }); 
    }); 
} 
+0

尝试了它所做的是在文件内容中添加每秒或更少的新行。 –

+0

你可以看到我的网站在这里:http://newsxpressmedia.com/ –

+0

Sridhar它的更新工作确实显示新的更新的内容,但在上面的一个新的HTML窗口中,我一直看到旧的内容也滚动。每60秒它不会销毁旧内容,只需添加一个新的滚动jquery即可。所以,我首先看到以下内容:例如,你好。在60秒之后,我看到:变好了。两者都在滚动。我希望更新后的内容将取代旧内容。在您的解决方案中,它将在旧版本中添加更新后的文本。 –

0

最新信息的功能。如果你想刷新页面从JavaScript每分钟然后

setInterval(function() { 
    //retrieve file from server and update html with new contents 
}, 60000) 

会做伎俩。但是,使用这种方法会每隔一分钟重新加载一次文件,即使该文件没有真正更改过。

你可以看看SignalR触发从服务器端的一切。你要做的是创建一个集线器,一旦这个文件的新版本上传完毕,它将发送一个通知给所有订阅的客户(其中一个将是你的网站)。这将确保仅当文件实际更改时才更新页面。

你可以阅读更多关于SignalR here

0

使用Javascript:

setTimeout(function() { 
    location.reload(); 
}, 300000);