【发布时间】:2014-03-25 07:51:33
【问题描述】:
我在 c# 应用程序中每隔几分钟更新一个文本文件,其中包含不同的数据。
例如第一次在文本文件中出现:Hello world 一分钟后,文本文件包含:大家好
现在在 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 中进行刷新?
【问题讨论】:
-
您要自动刷新还是手动(用户点击刷新)?
-
自动。例如,该站点将每分钟刷新一次。刷新或称之为更新。由于我每分钟都在更新文本文件,因此我希望网站也每分钟都读取更新的文件。它现在的方式一直显示旧文件内容。
-
我看到的重复示例将刷新整个窗口。当我浏览我的网站时,它每 5 秒刷新一次浏览器窗口。我不想要它,而是刷新/更新 javascript 函数,这样它就会调用更新后的上传文本文件。我尝试了重复的示例以及在这两种情况下都没有读取新文件 ocntent 的答案。它只是每秒调用一次新闻函数并将文本文件的相同旧内容添加到新行。
标签: c# javascript jquery ajax web