Click here to Skip to main content
15,500,453 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 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
{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}


What I have tried:

byte[] bytes = Encoding.ASCII.GetBytes(strBinary);
//Response.Buffer = true;
//Response.Charset = "";
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=" + fileName);

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

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));
Updated 9-Mar-21 4:37am
OriginalGriff 9-Mar-21 6:35am     CRLF
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     CRLF
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

Share this answer

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