For some reason, when I show the SaveAs dialog, my Checksum box never gets updated. However, if I comment out the Content-Disposition header code below then everything works fine. Any ideas?
protected void SubmitButton_Click(object sender, EventArgs e)
{
checksumField.Text = String.Empty;
FileInfo filename = this.CreateFileInfoFromInput();
if (!filename.Exists)
{
tableField.Focus();
tableField.BorderColor = System.Drawing.Color.Red;
return;
}
this.SaveFile(filename);
checksumField.Text = GetChecksum(filename);
}
private FileInfo CreateFileInfoFromInput()
{
/* blah */
return new FileInfo(blah);
}
private string GetChecksum(FileInfo filename)
{
return "test";
}
private void SaveFile(FileInfo fileName)
{
// Clear old headers
Response.Clear();
Response.Buffer = false;
// Set the ContentType and Headers
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName.Name); // <<<---- FAILING RIGHT HERE
Response.AppendHeader("Content-Length", fileName.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Connection", "Keep-Alive");
// Send data
Response.TransmitFile(fileName.FullName);
}
You are short-circuiting the the page lifecycle.
Instead of letting the page update itself and render to the response stream, you inject the file in the response
My advise would be to follow these steps :
When the user submit an ID, generate the file as you did (hope the process is short), update your textbox (this should work as you did) and add the link to the handler file servicing.
Just beware of the security implication. Don’t blindly send the file specified in the url (to avoid file path injection).