Note that there are some explanatory texts on larger screens.

plurals
  1. POTransmitFile and WriteFile Do not work
    text
    copied!<p>I use TransmitFile and WriteFile to Write an Excel File,but anyone do not work correctly my code is :</p> <pre><code>// Get the physical Path of the file(test.doc) string filepath = newFilePath; // Create New instance of FileInfo class to get the properties of the file being downloaded FileInfo file = new FileInfo(filepath); // Checking if file exists if (file.Exists) { // Clear the content of the response HttpContext.Current.Response.ClearContent(); // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", file.Name)); // Add the file size into the response header HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString()); // Set the ContentType HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) HttpContext.Current.Response.TransmitFile(file.FullName); } FileStream sourceFile = new FileStream(file.FullName, FileMode.Open); float FileSize; FileSize = sourceFile.Length; byte[] getContent = new byte[(int)FileSize]; sourceFile.Read(getContent, 0, (int)sourceFile.Length); sourceFile.Close(); HttpContext.Current.Response.ClearContent(); HttpContext.Current.Response.ClearHeaders(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; HttpContext.Current.Response.AddHeader("Content-Length", getContent.Length.ToString()); HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); HttpContext.Current.Response.BinaryWrite(getContent); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload