正常流程
正常的從服務器端下載文件的流程
System.IO.FileInfo file = new System.IO.FileInfo(s_path); HttpContext.Current.Response.ContentType = "application/ms-download"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8)); HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); HttpContext.Current.Response.WriteFile(file.FullName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); HttpContext.Current.Response.End();
調試
對於最后一句代碼HttpContext.Current.Response.End();
此語句執行后 后面的代碼就不會被執行。
查看Log日志中記錄的Exception的信息,顯示HttpContext.Current.Response.End()這條語句正在停止進程,也就是不會再執行后面的代碼。
總結
如果在調用該方法后還有要執行的代碼,就注釋掉最后一句代碼。
即為:
System.IO.FileInfo file = new System.IO.FileInfo(s_path); HttpContext.Current.Response.ContentType = "application/ms-download"; HttpContext.Current.Response.Clear(); HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream"); HttpContext.Current.Response.Charset = "utf-8"; HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(file.Name, System.Text.Encoding.UTF8)); HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); HttpContext.Current.Response.WriteFile(file.FullName); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.Clear(); //注釋該行代碼 //HttpContext.Current.Response.End();