我正在使用进度条显示正在上传的文件的进度。当我上传4个文件时,进度条停止在25%,即1循环迭代后。循环运行时它不会更新。 这是我使用的JavaScript代码。
<body>
<script type="text/javascript">
function updateProgress(percentage) {
var value = document.getElementById("pbar");
value.value = percentage;
}
</script>
<form id="form1" runat="server">
<div id="formbody" align="center">
<table style="width: 367px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="Enter Path:"></asp:Label>
</td>
<td>
<asp:TextBox ID="txtpath" runat="server"></asp:TextBox>
</td>
<td>
<asp:Button ID="Button2" runat="server" Text="Upload" OnClick="Button2_Click1"/>
</td>
</tr>
</table>
<br /><br />
<table>
<tr>
<td><progress id="pbar" width="70" max="100" min="0"></progress>
</td>
<td>
<asp:Label ID="lblstatus" runat="server" Text=""></asp:Label></td>
</tr>
</table>
</div>
</form>
这是我的c#代码,其中实现了进度条功能
private void SaveXml(string search)
{
if(!string.IsNullOrEmpty(txtpath.Text))
{
// double val = 0;
double count = 0;
int i = 1;
string temp=@"c:\Results";
string foldername = @"c:\";
if (Directory.Exists(temp))
{
Directory.Delete(temp, true);
}
string pathstring = Path.Combine(foldername, "Results");
Directory.CreateDirectory(pathstring);
string file = txtpath.Text.ToString();
string[] files = Directory.GetFiles(file);
int length = files.Length;
foreach (string eachfile in files)
{
DataSet ds = new DataSet();
ds.ReadXml(eachfile);
//Response.Write(file);
XmlDocument doc = new XmlDocument();
doc.Load(eachfile);
doc.PreserveWhitespace = false;
foreach (XmlNode n in doc)
{
if (n.NodeType == XmlNodeType.XmlDeclaration)
{
doc.RemoveChild(n);
}
}
string JsonText = JsonConvert.SerializeObject(doc);
JObject j = JObject.Parse(JsonText);
string str = j.ToString();
str = str.Replace(@"\r", " ");
str = str.Replace(@"\n", " ");
string newfile="JS"+i+".json";
var directory = new DirectoryInfo(pathstring);
var filecreated = new FileInfo(Path.Combine(directory.FullName, newfile));
//File.WriteAllText(@"C:\Results/" + newfile, JsonText);
using (Stream stream = filecreated.OpenWrite())
using (StreamWriter writer = new StreamWriter(stream))
{
writer.WriteLine(str);
}
i++;
//Page.ClientScript.RegisterStartupScript(this.GetType(),"","", true);
double val = (100 / length) + count;
count += val;
ClientScript.RegisterStartupScript(this.GetType(), "updateProgress", "updateProgress('" + val.ToString() + "');", true);
//Thread.Sleep(1000);
}
//val = length / 100;
Delete();
InsertintoDB(@"c:\Results");
//string updateProgress = "18";
//ClientScript.RegisterStartupScript(this.GetType(), "updateProgress", "updateProgress('" + updateProgress + "');", true);
}
else
{
Response.Write("No files found");
}
}