0

Updated:

I used .net 4.0 for coding, I want to find a pdf file with an specific path(ViewState["MediaFile"]) and transfer it to local system.

This is my code for File Transfer :

        //---server path ---
        String sFilePath = Server.MapPath("~/" + ViewState["MediaFile"].ToString());
        String sFileName= System.IO.Path.GetFileName(sFilePath);
        String RelativePath =sFilePath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty);
        HttpContext.Current.Response.ContentType = "APPLICATION/OCTET-STREAM";
        String Header = "Attachment; Filename=" + sFileName;
        HttpContext.Current.Response.AppendHeader("Content-Disposition", Header);
        System.IO.FileInfo Dfile = new System.IO.FileInfo(RelativePath);
        HttpContext.Current.Response.End();

but after running, there 's an error:

Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.

when I put HttpContext.Current.ApplicationInstance.CompleteRequest() instead of HttpContext.Current.Response.End() it's not error but do NOTHING!!! I mean a download panel is not shown.

Note: I test this code on another page,it works. just want to say is it possible because of any code in my form? I use Ajax UpdatePanel and disable right click on the form.

Sara N
  • 1,079
  • 5
  • 17
  • 45

2 Answers2

0

Response.End() should be enough :) You should also set the Content-Length header to the size of the file for the browsers to calculate the download progress.

Better than using Response.TransmitFile is to stream that file out, using filestream of that PDF and write that stream in chunks to the Response.OutputStream

var BlockSize = 4 * 1024 * 1024;
var buffer = new byte[BlockSize];
int bytesRead;
var outStream = Response.OutputStream;
while ((bytesRead = fileStream.Read(buffer, 0, BlockSize)) > 0)
{
    outStream.Write(buffer, 0, bytesRead);
    outStream.Flush();
}

BTW: the content type is not really related to the file extension. Means you shouldn't create the content type with the file extension.

Juergen Gutsch
  • 1,774
  • 2
  • 17
  • 28
  • Thank you for response, just want to know where's the download location will be? because when I traced it , it just pass without any location requested for saving. – Sara N May 26 '15 at 05:37
  • The download location will be specified by the user via the browsers file save dialogue or with the browsers default settings. This is depending on the browser and its settings. – Juergen Gutsch May 26 '15 at 05:40
  • then it gets the same issue. its not working for me. I'm about thinking maybe something set in my browser , or visual studio – Sara N May 26 '15 at 06:49
  • Could you please try to debug the C# code? If you don't get an exception the issue is on the client side (browser) You should also try a different browser – Juergen Gutsch May 26 '15 at 07:15
0

Try like this

Here's mediaUploader is my folder name where I have all my .pdf files. So now the code to download pdf file would be like as written below.

First will check ViewState is not null, then check whether the file exists, and then we download the file

ViewState["MediaFile"] ="mediaUploader/Visual2.pdf";

 if (ViewState["MediaFile"] != null)
 {
     String sFile = Server.MapPath("~/" + ViewState["MediaFile"].ToString());
     FileInfo file = new FileInfo(sFile);
     if (file.Exists)
     {
         Response.ContentType = "application/pdf";
         Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
         Response.TransmitFile(Server.MapPath("~/" + ViewState["MediaFile"].ToString()));
         Response.End();
     }
 }
Satinder singh
  • 10,100
  • 16
  • 60
  • 102
  • thanks , but why its not working for me? just the same exception – Sara N May 26 '15 at 05:39
  • @SarahN: I have added `Response.End()` instead of `HttpContext.Current.ApplicationInstance.CompleteRequest();` and set hardcode 'Response.ContentType' as you mention it would be pdf only. If my answer works for you , then do marked as answer – Satinder singh May 26 '15 at 05:48
  • I just dont know whats the difference between your answer and mine! they're exactly the same. I've tried both **HttpContext.Current.ApplicationInstance.CompleteRequest();** and **Response.End()** – Sara N May 26 '15 at 06:47
  • @SarahN: check your `ViewState["MediaFile"].ToString()` value, what you getting in it ? – Satinder singh May 26 '15 at 07:08
  • No Error in ViewState – Sara N May 27 '15 at 00:57
  • @SarahN: Debug your code and see if anything you get, Coz the above code works fine for me. debug and paste it here what you gets in `ViewState["MediaFile"]` – Satinder singh May 27 '15 at 06:29