Save file from image field of database

I have uploaded a text file to my database in the Image field. The problem is that I am trying to restore the file (so the user can download it by clicking on the link). When I try to load it, it just populates with the content of the webpage (all the html gets populated into a text file). I see this as a bug with the way I try to upload the file. Is it not possible to transfer content to a file without storing it in a temporary file?

The code I am using is shown below. UploadFiles is my class which contains data, id, name, etc.

    public void DownloadUploadedFile(Page sender, UploadFiles uf)
{
    sender.Response.ContentType = uf.FileType; // the binary data
    sender.Response.AddHeader("Content-Disposition", "attachment; filename=" + uf.FileName);
    sender.Response.BinaryWrite(uf.FileData);
}

      

+2


source to share


1 answer


It looks like you have too much of the rest of the page going to the client. You need to clear the answer and then close it; try:

sender.Response.Clear();
sender.Response.ContentType = uf.FileType; // the binary data
sender.Response.AddHeader("Content-Disposition", "attachment; filename="
         + uf.FileName);
sender.Response.BinaryWrite(uf.FileData);
sender.Response.Close();

      



Alternatively, use a handler (ashx) for this - as this does not include normal page markup.

+2


source







All Articles