Have you ever rendered and Excel, Word, or PDF document with ASP.NET and disabled the button when the user clicked "Export To Excel" so that they would only click it once? Then only to find out the the button is still disabled after Response.End() and you cannot enable it again?
Well the only way I found to enable the button again is with a JavaScript timer after x number of seconds. This is not the best solution if your process runs long. If you have found a better way please email me.
The code below will disable the button on an ASP.NET page after a user clicks it. Then a JavaScript will run and enable the button again after 10 seconds.
protected void Page_Load(object sender, EventArgs e)
{
BtnPrint.Attributes.Add("onclick", "javascript:"
+ BtnPrint.ClientID + ".disabled=true;"
+ Page.ClientScript.GetPostBackEventReference(BtnPrint, null));
//Renable the button after 10 seconds
string Script = @"";
Script = Script.Replace("$BTNAME", BtnPrint.ClientID);
Page.ClientScript.RegisterStartupScript(GetType(), "script", Script);
}
Once you store a Word Document or PDF Document in your database as a BLOB you then need to convert it to a Byte Array in order for the browse to launch Word or Adobe to open the PDF. This example pulls either the Word or PDF version from a table.
//Select either the Word or PDF version
byte[] ProposalDoc;
string ContentType = string.Empty;
string Extension = string.Empty;
if (asWord)
{
ProposalDoc = row.PROPOSAL_WORD;
ContentType = "application/word";
Extension = ".doc";
}
else
{
ProposalDoc = row.PROPOSAL_PDF;
ContentType = "application/pdf";
Extension = ".pdf";
}
MemoryStream MemStream = new MemoryStream(ProposalDoc);
CTX.Response.ContentType = ContentType;
CTX.Response.AddHeader("content-disposition",
"attachment; filename=" + DocumentName + Extension);
//Response.AddHeader("Content-Length", oStream.Length.ToString());
CTX.Response.Clear();
CTX.Response.Buffer = true;
CTX.Response.BinaryWrite(MemStream.ToArray());
CTX.Response.End();
The ASPOSE document object is not searializeable. In order to save the object to the database you first have to convert it to a memory from the object (such as PDF of Word). Once you have it in a Byte Array you can updated a BLOB column in the database.
//Convert to WORD
MemoryStream outStream = new MemoryStream();
asposeDocument.Save(outStream, Aspose.Words.SaveFormat.Doc);
byte[] docBytes = outStream.ToArray();
//Convert to PDF
outStream = new MemoryStream();
asposeDocument.Save(outStream, Aspose.Words.SaveFormat.Pdf);
byte[] pdfBytes = outStream.ToArray();