1

我想知道有没有人知道这个。

我在我的 asp.net vb 网络应用程序上遇到了一个非常严重的错误。我告诉你,我的额头上差点被 qwerty 染上了颜色,因为我的头撞在了键盘上。但我想我找出了错误的原因:

我有以下代码:

 fileToDownload = createFileSet(fileToDownload, confirm)
                    HttpContext.Current.Response.ClearContent()
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileToDownload.Name)
                    HttpContext.Current.Response.AddHeader("Content-Length", fileToDownload.Length.ToString())
                    HttpContext.Current.Response.ContentType = ReturnExtension(fileToDownload.Extension.ToLower())
                    HttpContext.Current.Response.TransmitFile(fileToDownload.FullName)
                    HttpContext.Current.ApplicationInstance.CompleteRequest()
                    Directory.Delete("C:\temp_" & confirm, True)
                    'System.IO.File.Delete(fileToDownload.FullName) <--- Error here

当我把最后一行的注释打勾时,我得到了一个非常丑陋的错误(我可以重做它并把它拼出来,什么样的错误,但这并不重要)。

我想我知道原因。用户正在下载文件,或正在下载文件,或其他内容,并且程序正在尝试删除它......导致错误。

有没有办法创建一些on HttpContext.Current.Response.TransmitFile.complete(代码组成),还是我求助于批处理文件和 Windows 调度程序来清理这些不再需要的文件?

你认为我的错误结论正确吗?我真的看不到创建“活动”的方法,但我在 asp.net 上太新了,无法确定。

4

1 回答 1

0

尝试使用 finally 块(从这里得到想法http://forums.asp.net/t/1436818.aspx/1)。简而言之,这样做...

try {
 fileToDownload = createFileSet(fileToDownload, confirm)
                    HttpContext.Current.Response.ClearContent()
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileToDownload.Name)
                    HttpContext.Current.Response.AddHeader("Content-Length", fileToDownload.Length.ToString())
                    HttpContext.Current.Response.ContentType = ReturnExtension(fileToDownload.Extension.ToLower())
                    HttpContext.Current.Response.TransmitFile(fileToDownload.FullName)
                    HttpContext.Current.ApplicationInstance.CompleteRequest()
                    Directory.Delete("C:\temp_" & confirm, True)
} finally {
    System.IO.File.Delete(fileToDownload.FullName)
}

或者这样做:

您可以将 zip 文件内容读入内存流,删除文件然后将内存流推送到响应缓冲区。我能看到的唯一问题是下载失败并且他们想再次尝试获取它。无论如何,这里有一篇文章向您展示如何处理文件和内存流并将其推送到响应缓冲区http://www.c-sharpcorner.com/UploadFile/jhblankenship/DownloadingFromMemStream11262005060834AM/DownloadingFromMemStream.aspx

于 2012-08-22T16:05:28.923 回答