0

I have a file, and it is full path (plus the file name) is in this variable:

fileTemporary

i want to download that file to the client.

i do this:

 HttpContext.Current.Response.TransmitFile(fileTemporary);

but nothing happen, i mean when i click the button, this file executes, but nothing is being downloaded to the client. i don't see any file on the browser of the client.

what mistake did i do please?

Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253
  • 1
    Have you set a breakpoint on this line to be sure it gets hit? Does `fileTemporary` exist and have you specified the path correctly? Does your website/application have rights to that file and directory? Is the file empty? – Bob Kaufman Sep 05 '15 at 14:35
  • What browser are you using? I'd use either something like FireBug, IE's F12 network monitor, or Fiddler to see what is actually being sent to your browser. I'd do that after confirming you receive no errors on the C# side. – dmeglio Sep 05 '15 at 14:35
  • @BobKaufman yes i did a breakpint and it reaches there. and yes fileTemporary exist. and first I didn't have the permision, so i was getting error that i don't have permission to that file, but then i made that file for all users and then i see that nothing is being downloaded – Marco Dinatsoli Sep 05 '15 at 14:37
  • When you say "full path", are you talking physical path on the server? (That's what it should be, not a URL to the file) – freefaller Sep 05 '15 at 14:37
  • @dman2306 google chrome, – Marco Dinatsoli Sep 05 '15 at 14:38
  • @freefaller i meant full path on the server, like `c:\user\marco\.....myfile` – Marco Dinatsoli Sep 05 '15 at 14:39
  • @MarcoDinatsoli then use Chrome's network monitor to see what the browser is receiving. See if the file contents is there. Also, hopefully not the case - you're sure the file isn't empty, right? – dmeglio Sep 05 '15 at 14:40
  • @dman2306 i see now, there is this error `Uncaught Sys.WebForms.PageRequestManagerParserErrorException: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed.` on the console. could you help please? – Marco Dinatsoli Sep 05 '15 at 14:42
  • It seems to rather be a client script error than a problem with TransmitFile. You should rework your question – Simon Mourier Sep 05 '15 at 14:48

1 Answers1

1

If you use MVC you can:

[HttpGet]
        public virtual ActionResult GetFile(string fileTemporary)
        {
            // ...preparing file path... init fileTemporary.

            var bytes = System.IO.File.ReadAllBytes(fileTemporary);
            var fileContent = new FileContentResult(bytes, "binary/octet-stream");
            Response.AddHeader("Content-Disposition", "attachment; filename=\"YourFileName.txt\"");

            return fileContent;
        }

If you use ASP.NET or whatever you can use following (sorry, my old code, but you can understand approach):

var bytes = System.IO.File.ReadAllBytes(fileTemporary);
SendFileBytesToResponse(bytes, fileName);

public static bool SendFileBytesToResponse(byte[] bytes, string sFileName)
        {
            if (bytes!= null)
            {
                string downloadName = sFileName;
                System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
                response.Clear();
                response.AddHeader("Content-Type", "binary/octet-stream");
                response.AddHeader("Content-Disposition",
                                   "attachment; filename=" + downloadName + "; size=" + bytes.Length.ToString());
                response.Flush();
                response.BinaryWrite(bytes);
                response.Flush();
                response.End();
            }

            return true;
        }

Without reingeneering your solution:

System.Web.HttpContext.Current.Response.Clear();

System.Web.HttpContext.Current.Response.AddHeader("Content-Type", "binary/octet-stream");
System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition",
                "attachment; filename=" + fileName);  

System.Web.HttpContext.Current.Response.TransmitFile(fileName);

If you would like browser to interpret you file right, you will need to specify header "Content-Type" more precise. Please see list of content types

Anton Norko
  • 2,166
  • 1
  • 15
  • 20