开发者

ASP.NET strange server error on file download

开发者 https://www.devze.com 2023-01-11 06:23 出处:网络
I\'ve got very strange error when trying download zip file, generated on the fly. On some client computers everything is OK, file downlading perfect, on others there is ser开发者_开发百科ver exception

I've got very strange error when trying download zip file, generated on the fly. On some client computers everything is OK, file downlading perfect, on others there is ser开发者_开发百科ver exception (I am trying to test using different browsers):

Message: Object reference not set to an instance of an object StackTrace: в ASP.development_detail_aspx.__RenderContent1(HtmlT­extWriter __w, Control parameterContainer) в System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) в System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) в System.Web.UI.HtmlControls.HtmlForm.RenderChildren­(HtmlTextWriter writer) в System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTex­tWriter output) в System.Web.UI.HtmlControls.HtmlForm.RenderControl(­HtmlTextWriter writer) в System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) в System.Web.UI.Control.RenderChildrenInternal(HtmlT­extWriter writer, ICollection children) в System.Web.UI.Page.Render(HtmlTextWriter writer) в System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

My code in asp:ImageButton OnClick event. Server is IIS 7.5:

...

using (MemoryStream output = new MemoryStream())
{
    using (ZipOutputStream s = new ZipOutputStream(output))
    {
        s.SetLevel(5); // 0 - store only to 9 - means best compression
        byte[] buffer = new byte[4096];


        foreach (var file in devFiles)
        {
            if (file.PhisicalName != "")
            {
                ZipEntry entry = new ZipEntry(file.RealName);
                s.PutNextEntry(entry);
                using (FileStream streamer = File.OpenRead(HttpContext.Current.Server.MapPath(file.PhisicalName)))
                {
                    int sourceBytes;
                    do
                    {
                        sourceBytes = streamer.Read(buffer, 0, buffer.Length);
                        s.Write(buffer, 0, sourceBytes);
                    }
                    while (sourceBytes > 0);
                } 
            }
        }

    s.Finish();    
    HttpResponse response = HttpContext.Current.Response;
    response.Clear();
    response.ContentType = "application/octet-stream";
    response.AppendHeader("Content-Disposition", "attachment; filename=doc.zip");
    response.AppendHeader("Content-Length", output.Length.ToString());
    response.BinaryWrite(output.ToArray());
    response.End();
    output.Close();
    s.Close();
}


I make an assume here and I am not 100% sure.

The response.End() is throw an exception, and after that probably the rest page is go randomly ! because you (and I) do not know where the exception is jump. So the close file command is bypassed !.

I suggest to remove the End(), and use the Flush()

Also I suggest to use the using(){} for the file open close, and maybe a buffering read of the file due to memory problems if the zip file is too big.

Also with this code you have open files left, so maybe the output in the next cycle is null because can not open the file...


Finaly, find the answer. It was very simple/stupid. Above in the code there was a check if user was logged in. If not there was nothing to render. For people who was logged in admin interface everithing was OK, but others got Render exception. Thanks everybody!

0

精彩评论

暂无评论...
验证码 换一张
取 消