-2

I have in c# application that update a text file each few minutes with a different data inside.

For example first time in the text file there: Hello world After a minute the text file contain: Hi everyone

Now in the c# application i'm uploading the text file once it was change.

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();
            }
        }
    }
}

I see on my hard disk the text file is changed the content and also after uploading the file to my website ftp i see there the updated text file.

Now in my website i'm using javascript/ajax to read the uploaded text file:

<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>

The problem is once my application in c# updated the file and uploaded it to the ftp i need in my browser for example chrome to make manual refresh if not it will keep showing the old text file content and not the updated.

How can i make a refresh maybe in the javascript ?

Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • you want an automatic refresh or manual (user clicks refresh) ? – Szilard Muzsi Mar 25 '14 at 07:55
  • Automatic . For example that the site will be refresh every minute. Refresh or to call it updating. Since im updating the text file every minute i want the site to read the updated file also every minute. The way it is now it keep showing the old file content. – Daniel Lip Mar 25 '14 at 07:57
  • 1
    possible duplicate of [How to reload page every 5 second?](http://stackoverflow.com/questions/2787679/how-to-reload-page-every-5-second) – Shaharyar Mar 25 '14 at 07:58
  • The duplicated example i saw it it will refresh the whole window . When i surf to my website it's refreshing the browser window every 5 seconds. I dont want it but to refresh/update the javascript function so it will call the updated uploaded text file. I tried the duplicated example and also the answer here in both cases it didn't read the new file ocntent. It's just calling the news function every second and adding a the same old content of the text file to a new line. – Daniel Lip Mar 25 '14 at 08:23

4 Answers4

3

Try like this

No need to refresh the page just refresh the function that will get the latest info from text file

$(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
        });
    });
}
Sridhar R
  • 20,190
  • 6
  • 38
  • 35
  • Tried it what it does is adding every second or less a new line with the file content. – Daniel Lip Mar 25 '14 at 08:08
  • Sridhar it's working with the updating it does show the new updated content but in a new html window above i keep see the old content scrolling up too. Every 60 second it's not destroying the old content place just add a new scrolling jquery . So first i see the old content for exmaple: Hello. After 60 second under it i see: changed good. Both are scrolling. I want that the updated content will replace the old one. In your solution it's adding the updated text under the old one. – Daniel Lip Mar 25 '14 at 08:36
  • @DanielLip oh k,now i clear the old data and process the function check updated code – Sridhar R Mar 25 '14 at 08:43
  • Still not working. Now what it does is showing part of the new updated content in a new window it didnt clear the old one and didnt overwrite it. And also near the new content now i see diamond with '?' inside in the new content. Thats when i'm browsing to my site. – Daniel Lip Mar 25 '14 at 08:47
0

If you want to refresh the page every minute from JavaScript then

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

will do the trick. However, using this approach will reload the file every minute, even if the file hasn't actually been changed.

You could look into SignalR to trigger everything from the server side. What you would do is create a hub which, once a new version of the file has been uploaded, will send a notification to all subscribed clients (one of which will be your website). This will ensure that the page gets updated only when the file actually changes.

You can read more about SignalR here.

Szilard Muzsi
  • 1,881
  • 2
  • 16
  • 20
0

Simply use asp:timer control and puts your content in UpdatePanel that's it.

you can also see example for timer and set time as per your time requirement.

http://msdn.microsoft.com/en-us/library/bb398865(v=vs.100).aspx

5Star
  • 61
  • 7
0

Using javascript:

setTimeout(function() {
    location.reload();
}, 300000);
Karim AG
  • 2,166
  • 15
  • 29