1

I have file that i converted and inserted it to my DB:

byte[] file = File.ReadAllBytes(outputpath);
string filesting = Convert.ToBase64String(file);
//INSERT INTO DB

Then I pull it from the DB and try to download it.

byte[] bytes = Convert.FromBase64String(file);
HttpContext.Current.Response.ContentType = "application/force-download";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=labtest.avi");
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.End();

But nothing is downloaded.

Why?

Grigory Zhadko
  • 1,484
  • 1
  • 19
  • 33
jacky brown
  • 271
  • 2
  • 7
  • 20

1 Answers1

5

Try using Response.BinaryWrite() and getting rid of the MemoryStream. The following code worked in my test (though I loaded a file from a resx resource, but as a byte array):

Response.ContentType = "application/force-download";
Response.AppendHeader("Content-Disposition", "attachment;filename=labtest.avi");

byte[] bytes = Convert.FromBase64String(file);
Response.BinaryWrite(bytes);

Response.End();
bassfader
  • 1,346
  • 1
  • 11
  • 15
  • Then we'll need some more information. Are you using the exact code I posted (so without the "MemoryStream", "Response.Buffer = true" and "Response.Clear()" stuff you used before)? Then where exactly are you using that code, and whats happening when it executes? I tested in a simple button OnClick handler, and it prompted me to download when clicking it... Do you see the file contents or just an empty page when trying? Are you sure the file gets retrieved from the DB correctly? – bassfader Apr 11 '16 at 14:04
  • yes. i using exact code you posted. but in the try catch i get error message - Thread was being aborted. – jacky brown Apr 12 '16 at 06:33
  • I don't see any try catch in your example, could you please add some more information to your post? Also very important: What exception exactly did you get, and where is it thrown? As I mentioned, the code I wrote in my answer works in my test, I only replaced "Convert.FromBase64String(file)" with a file stored as a byte-array in a resx file; so there must be some other kind of error. – bassfader Apr 12 '16 at 07:22
  • i edit my code. you can see that i changed it to your code and added try catch. the error in the catch is: "Thread was being aborted." – jacky brown Apr 12 '16 at 10:59
  • 1
    The `Thread was being aborted` exception is most likely thrown by the call to `Response.End()` (as it always throws that exception, [see this MSDN article](https://msdn.microsoft.com/en-us/library/system.web.httpresponse.end(v=vs.110).aspx)). I tested with an added try-catch, but even when catching the exception my code works for me. So there still must be some other kind of error somwhere else. Could you please be more specific about your problem? (How and where exactly are you calling that code? What are you doing before and after that call? Have you tried to simplify your code? etc...) – bassfader Apr 13 '16 at 11:32
  • Please try to provide a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) so that others can help you – bassfader Apr 13 '16 at 11:34