0

I'm working on a functionality in my asp.net web site that enables the user to download some files as a zip file. I'm using the DotNetZip library to generate the zip file.

My code looks like this:

protected void OkbtnZipExport_OnClickEvent(object sender, EventArgs e)
{
        var selectedDocumentIds = GetSelectedDocIds();

        string archiveName = String.Format("archive-{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));            
        AddResponseDataForZipFile(Response, archiveName);            

        try
        {
            string errorMessage = Utils.ExportToZip(selectedDocumentIds, arkivdelSearchControl.GetbraArkivConnection(), Response.OutputStream);
            if (!string.IsNullOrEmpty(errorMessage))
            {
               LiteralExportStatus.Text = errorMessage;                                                     
            }
            else
                LiteralExportStatus.Text = "Success";

        }
        catch (Exception ex)
        {
            LiteralExportStatus.Text = "Failure " + ex.Message;                
        }

        Response.Flush();
        Response.Close();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
}

private void AddResponseDataForZipFile(HttpResponse response, string zipName)
{
        Response.Clear();
        Response.BufferOutput = false;
        Response.ContentType = "application/x-zip-compressed";
        Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
        Response.AddHeader("Expires", "0");
        Response.AddHeader("Content-Description", "Zip Arcive");
}

Now, if anything goes wrong, say the Utils.ExportToZip method fails, I want to present an error message to the user and not the download dialog. Do I have to remove some data from the Response object in order to cancel the download operation?

Best regards

OKB

4

2 回答 2

1

I can't comment at the moment, so take this answer as one.

How does Utils.ExportToZip work? If the reason it takes the Response.OutputStream for the constructor is to write the zip-file directly into it, then you need to set Buffering in order to "undo" that in your AddResponseDataForZipFile Method:

Response.BufferOutput = true;
于 2010-10-29T11:24:08.510 回答
1

first, Don't call HttpContext.Current.ApplicationInstance.CompleteRequest();

Reference.

At one point, there was some example code that showed CompleteRequest(), but it's wrong.

Second - to do what you describe, you'll need to insure that the zip file can be created correctly and in its entirety, before sending anything. That means you should do the AddResponseDataForZipFile() only after the zipfile is completely created. That means you need to create an actual zip file on the server, and not simply save out to Response.OutputStream. Once the file is successfully created, then call AddResponseDataForZipFile(), stream the bytes for the temp zip file, call Response.Close(), then delete the temporary zip file.

于 2010-11-08T02:59:45.533 回答