Click here to Skip to main content
15,031,347 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am facing an issue with the download file in the browser using base64 or binary string. On button click event, I am calling API on the server side and trying to return the file without saving it in the physical folder.

Q. Is it possible to download the file without saving it in the physical folder?
Q. If possible, what are the ways to do it in the asp.net webform?

I am stuck with lots of trials, any help would be appreciated.

** I have got the actual file, when I was trying to consume that API using postman, there is an option(while sending Request) to Send and Download

Q. How can that be done(like postman is allowing to download the file) using c#?

Note: I am getting base64 or binary from an API.

Exception on
Response.End();
:
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}


Thanks

What I have tried:

C#
byte[] bytes = Encoding.ASCII.GetBytes(strBinary);
//Response.Buffer = true;
//Response.Charset = "";
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
//Response.BinaryWrite(bytes);
//Response.Flush();
//Response.End();

//Response.Clear();
//MemoryStream ms = new MemoryStream(bytes);
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
//Response.Flush();
//Response.Buffer = true;
//ms.WriteTo(Response.OutputStream);
//Response.Flush();

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
HttpContext.Current.Response.AddHeader("Content-Length", Convert.ToString(bytes.Length));
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();
Posted
Updated 9-Mar-21 3:37am
v4
Comments
OriginalGriff 9-Mar-21 6:35am
   
And?
What does it do that you didn't expect, or not do that you did?
What have you tried to do to find out why?
Are there any error messages, and if so, where and when? What did you do to make them happen?

This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
Use the "Improve question" widget to edit your question and provide better information.
itsathere 9-Mar-21 6:43am
   
An exception occurs on Response.End();
and the exception is
ex = {Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}

If you need more information, I can share that too.
Richard Deeming 10-Mar-21 11:17am
   
NB: Encoding.ASCII.GetBytes(strBinary) is almost certainly the wrong method to use. What is the actual value of your strBinary variable?
itsathere 16-Mar-21 9:30am
   
binary string

1 solution

   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900