如何解决从加载动画转换为 ASP.Net webforms 页面中文件下载的进度条
我在下载文件和为用户提供真正的进度条以进行更长时间下载的示例方面能够找到的大部分内容都已损坏。我目前正在使用的是带有 UpdateProgress 控件 wiloading gif 的 UpdatePanel:
<asp:UpdateProgress ID="UpdateProgress1" AssociatedUpdatePanelID="UpdatePanel1" runat="server">
<Progresstemplate>
<asp:Panel ID="loadingPanel" runat="server" Style="vertical-align: middle; height: 100%; background-image: url('../Images/ajax_loading.gif'); background-position: center center; width: 100%; background-repeat: no-repeat; background-color: #FFFFCC; text-align: center; opacity: 0.4; -moz-opacity: 0.8; filter: alpha(opacity=80); left: 0; top: 0; position: fixed; z-index: 99; background-attachment: fixed;"> </asp:Panel>
</Progresstemplate>
</asp:UpdateProgress>
一个脚本然后加载一个页面以通过 JS 在 iFrame 中进行实际下载:
<script lang="javascript">
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
function InitializeRequest(sender,args) {
if (sender._postBackSettings.sourceElement.id == "btnDownloadAll") {
var iframe = document.createElement("iframe");
iframe.src = "GenerateDownloadFile.aspx?filetype=all";
iframe.style.display = "none";
document.body.appendChild(iframe);
}
else if...
}
</script>
GenerateDaownloadFile.aspx 做了一些工作来将数据拉到一起并压缩它:
private bool BuildZipFileForAll(Guid UserId,string category,string quarter)
{
bool hasError = false;
try
{
string categoryzipFilePath = Session["ZipFilePath"].ToString();
string CategoryZipFileName = Session["ZipFileName"].ToString();
string CategoryZipFolder = CategoryzipFilePath.Substring(0,CategoryzipFilePath.Length - CategoryZipFileName.Length);
string zipName = string.Format(category + "-" + quarter + ".zip");
DataTable dr = (DataTable)Session["FileNamesList"];
string folder = DateTime.Now.ToString("mm_ss.fff")+ "_" + category;
string tempZipFolder = Path.Combine(Global.main_data_directory,@"CategoryZip",folder);
Directory.CreateDirectory(tempZipFolder);
string destZipFilePath = Path.Combine(tempZipFolder,CategoryZipFileName);
string FolderPath = Path.Combine(tempZipFolder,"DoD_Condensed_Files");
Directory.CreateDirectory(FolderPath);
for (int l = 0; l < dr.Rows.Count; L++)
{
string fileName = dr.Rows[l]["FileName"].ToString();
WriteFile(FolderPath,fileName);
}
using (var mainZip = new ZipFile())
{
File.copy(zipFilePath,destZipFilePath);
mainZip.AddSelectedFiles("*.*",tempZipFolder,"",true);
Response.Clear();
Response.BufferOutput = false;
Response.ContentType = "application/zip";
response.addheader("Content-disposition","attachment; filename=" + zipName);
mainZip.Save(Response.OutputStream);
Directory.Delete(tempZipFolder,true);
}
}
}
catch (Exception ex)
{
DAL.ErrorLog("BuildZipFile: " + ex.Message);
hasError = true;
}
return hasError;
}
以这种方式生成的某些文件可能需要 20 秒以上的时间来编译并提示下载,因此我希望创建一个进度条指示以向用户提供有用的反馈。任何提示都会有所帮助!
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。