引用我的较早前的问题关于对从服务器下载文件并正确处理异常。我肯定,我已经这样解决了,那么经典的编程方式,返回天后令人沮丧的发现它破: - (


<强>更新的代码:

private static void GoGetIt(HttpContext context)
    {
        var directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe = new FileInfo(......);
                var name = SomeBasicLogicToDecideName();

            //if (!directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe.RefreshExists())
            //{
            //  throw new Exception(string.Format("Could not find {0}.", name));
            //}

            var tempDirectory = ""; //Omitted - creates temporary directory

            try
            {
                directoryInfoOfWhereTheDirectoryFullOfFilesShouldBe.CopyAll(tempDirectory);
                var response = context.Response;
                response.ContentType = "binary/octet-stream";
                response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.zip", name));
                ZipHelper.ZipDirectoryToStream(tempDirectory, response.OutputStream);
                response.End();
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.ToString());
                context.Response.StatusCode = 404;
            }
            finally
            {
                tempDirectory.DeleteWithPrejudice();
            }
        }

这是工作的罚款,并返回拉链,否则,如果该文件不存在在客户端,我可以处理这个返回404然后:

public bool Download()
{   
 try
                {
                    using (var client = new WebClient())
                    {
                        client.DownloadFile(name, tempFilePath);
                    }

                }
                catch (Exception)
                {
                    fileExists = false;
                }
return fileExists;
    }

但是现在的问题是两件事情。

1)I GET System.Threading.ThreadAbortException:线程已被中止在服务器侧try-catch块。通常这只是未发现异常的文件。我不知道为什么新的异常抛出或什么?

2)现在,不同的异常在服务器端没有找到该文件,而不是扔,似乎我不能使用这组为应用程序,因为在客户端回来,假设将任何异常filenotfound。

任何帮助,为什么这ThreadAbortException抛出特别的信息!?!?不胜感激。干杯

有帮助吗?

解决方案

的问题是,到Response.End()抛出一个ThreadAbortException:这就是如何结束请求。刚刚摆脱这一呼吁干脆,你不需要它。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top