结果在我的最终应用程序中,我没有使用 Web 服务,而是使用简单的 AJAX 和 ASPX 页面,这就是我完成它的方式!
默认.ASPX 文件
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CloudWF._Default"
Culture="auto" meta:resourcekey="PageResource1" UICulture="auto" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link type="text/css" href="css/redmond/jquery-ui-1.8.21.custom.css" rel="stylesheet" />
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="jsEng" runat="server">
<Scripts>
<asp:ScriptReference Path="~/js/jquery-1.7.2.min.js" />
<asp:ScriptReference Path="~/js/jquery-ui-1.8.21.custom.min.js" />
</Scripts>
</asp:ScriptManager>
<input type='submit' value='Process New Records' id='trigger' onclick='BeginProcess(); return false;' />
</div>
</form>
</body>
<script type="text/javascript">
var btnStart;
$(function () {
btnStart = $("#trigger").button();
imgLoad = $("#loader").hide();
});
function BeginProcess() {
btnStart.val("Collecting Data...");
//to get the value of the radiobuttons
//alert($('input[name=A]:checked').val()) --> [name=*] where *= whatever the name of the radioset
// Create an iframe.
var iframe = document.createElement("iframe");
// Point the iframe to the location of
// the long running process.
iframe.src = "process.aspx";
// Make the iframe invisible.
iframe.style.display = "none";
// Add the iframe to the DOM. The process
// will begin execution at this point.
document.body.appendChild(iframe);
btnStart.val("Processing...");
imgLoad.show();
btnStart.button("disable");
}
function UpdateProgress(RecordComplete, Message, step) {
btnStart.val("Downloaded Record " + RecordComplete + Message);
$("#progressbar").progressbar({
value: step
});
if (step >= 100) {
imgLoad.hide();
btnStart.val("Download Complete!");
}
}
</script>
进程.aspx.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
public partial class process : System.Web.UI.Page
{
WebClient wc = new WebClient();
Dictionary<string, string> AudioURLs = new Dictionary<string, string>();
protected void Page_Load(object sender, EventArgs e)
{
// Padding to circumvent IE's buffer*
Response.Write(new string('*', 256));
Response.Flush();
ProcessRecords();
}
public void ProcessRecords()
{
int recTotal;
recordList = (IQueryable<Record>)(Session["UNPROCESSED"]);
recTotal = recordList.Count();
if (recordList.Count() > 0)
{
foreach (Record record in recordList)
{
try
{
curRow++;
step = ((decimal)curRow / (decimal)recTotal) * 100;
CreateDictionary();
CreateFolderwithAudios(record.tSessionID);
//record.processed = true;
db.SubmitChanges();
UpdateProgress(curRow, " of " + recTotal, step);
}
catch (Exception x)
{
HttpContext.Current.Response.Write("Exception: " + x.Message);
}
}
Session["UNPROCESSED"] = null;
}
}
public Dictionary<string, string> CreateDictionary()
{
AudioURLs.Clear();
#region Add Values to Dictionary
return AudioURLs;
}
public void DownloadAudios(string _subFolder, Dictionary<string, string> _AudioSources)
{
foreach (KeyValuePair<string, string> kvp in _AudioSources)
{
if (kvp.Value.StartsWith("http://"))
{
try
{
wc.DownloadFile(kvp.Value, audiosPath + "\\" + _subFolder + "\\" + kvp.Key + ".wav");
}
catch (WebException webex)
{
throw new WebException(webex.Message);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
}
}
public void CreateFolderwithAudios(string folderName)
{
try
{
//create the folder where the audios are going to be saved
Directory.CreateDirectory(audiosPath + folderName);
//create and read the Dictionary that contains the URLs for the audio files
DownloadAudios(folderName, AudioURLs);
}
catch (AccessViolationException ae)
{
HttpContext.Current.Response.Write(ae.Message);
}
catch (System.Exception x)
{
HttpContext.Current.Response.Write(x.Message);
}
}
protected void UpdateProgress(int PercentComplete, string Message, decimal step)
{
// Write out the parent script callback.
Response.Write(String.Format(
"<script>parent.UpdateProgress({0}, '{1}',{2});</script>",
PercentComplete, Message, step));
// To be sure the response isn't buffered on the server.
Response.Flush();
}