Response.TransmitFile Doesn't load or throw errors

I am currently using HttpResponse to download files from my server. I already have a couple of functions used to download Excel / Word files, but it's hard for me to get my simple text file (.txt) to load.

With a text file, I basically dump the contents of the TextBox into a file, trying to load the file from HttpResponse, and then delete the temporary text file.

Here is an example of my code that works for Excel / Word documents:

protected void linkInstructions_Click(object sender, EventArgs e) { String FileName = "BulkAdd_Instructions.doc"; String FilePath = Server.MapPath("~/TempFiles/BulkAdd_Instructions.doc"); System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType = "application/x-unknown"; response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ";"); response.TransmitFile(FilePath); response.Flush(); response.End(); } 

And here is a piece of code that doesn't work.
Whereas the code works without any errors. The file is created and deleted, but never dumped to the user.

 protected void saveLog(object sender, EventArgs e) { string date = DateTime.Now.ToString("MM_dd_yyyy_hhmm"); // Get Date/Time string fileName = "BulkLog_"+ date + ".txt"; // Stitch File Name + Date/Time string logText = errorLog.Text; // Get Text from TextBox string halfPath = "~/TempFiles/" + fileName; // Add File Name to Path string mappedPath = Server.MapPath(halfPath); // Create Full Path File.WriteAllText(mappedPath, logText); // Write All Text to File System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ClearContent(); response.Clear(); response.ContentType = "text/plain"; response.AddHeader("Content-Disposition", "attachment; filename=" + fileName); response.TransmitFile(mappedPath); // Transmit File response.Flush(); System.IO.File.Delete(mappedPath); // Delete Temporary Log response.End(); } 
+6
c # text-files
source share
5 answers

I decided to solve the problem myself. Turns out it was an Ajax issue preventing my Button from returning correctly. This stopped the TransmitFile transfer.

Thanks for the help!

-5
source share

This is because you delete the file before sending.

From MSDN - HttpResponse.End Method

Sends all current buffered output to the client, terminates the page, and raises the EndRequest event.

Try putting your System.IO.File.Delete (mappedPath); line after the answer .End (); in my test, it was then that it worked.

Also, it might be a good idea to check if the file exists in the first place, I don't see any file. There are exceptions, exceptions of null support exceptions and setting Content-Length.

EDIT: here is the code that I used in the project at work some time ago may help you a bit.

 // Get the physical Path of the file string filepath = System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath + folder + filename; // 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 Response.ClearContent(); // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header Response.AddHeader("Content-Disposition", String.Format("attachment; filename={0}", file.Name)); // Add the file size into the response header Response.AddHeader("Content-Length", file.Length.ToString()); // Set the ContentType Response.ContentType = ReturnFiletype(file.Extension.ToLower()); // Write the file into the response (TransmitFile is for ASP.NET 2.0. In ASP.NET 1.1 you have to use WriteFile instead) Response.TransmitFile(file.FullName); // End the response Response.End(); //send statistics to the class } 

And here is the Filetype method that I used

 //return the filetype to tell the browser. //defaults to "application/octet-stream" if it cant find a match, as this works for all file types. public static string ReturnFiletype(string fileExtension) { switch (fileExtension) { case ".htm": case ".html": case ".log": return "text/HTML"; case ".txt": return "text/plain"; case ".doc": return "application/ms-word"; case ".tiff": case ".tif": return "image/tiff"; case ".asf": return "video/x-ms-asf"; case ".avi": return "video/avi"; case ".zip": return "application/zip"; case ".xls": case ".csv": return "application/vnd.ms-excel"; case ".gif": return "image/gif"; case ".jpg": case "jpeg": return "image/jpeg"; case ".bmp": return "image/bmp"; case ".wav": return "audio/wav"; case ".mp3": return "audio/mpeg3"; case ".mpg": case "mpeg": return "video/mpeg"; case ".rtf": return "application/rtf"; case ".asp": return "text/asp"; case ".pdf": return "application/pdf"; case ".fdf": return "application/vnd.fdf"; case ".ppt": return "application/mspowerpoint"; case ".dwg": return "image/vnd.dwg"; case ".msg": return "application/msoutlook"; case ".xml": case ".sdxl": return "application/xml"; case ".xdp": return "application/vnd.adobe.xdp+xml"; default: return "application/octet-stream"; } } 
+10
source share

Thank you for continuing your problem. I spent hours trying to understand why the error code was not thrown, even though nothing happened. It turns out that this is my AJAX UpdatePanel mysteriously and covertly interfered.

+2
source share

I stumbled upon this message in my search and noticed that it was not helpful to tell us why UpdatePanel caused the problem in the first place.

UpdatePanel is an asynchronous postback, and Response.TransmitFile requires a full postback to work properly.

The control that triggers the asynchronous postback must be running in the UpdatePanel:

 <Triggers> <asp:PostBackTrigger ControlID="ID_of_your_control_that_causes_postback" /> </Triggers> 
+1
source share

Also try this to save text on the client side (only now Chrome) without a round trip to the server.

Here is another flash base ...

0
source share

All Articles