I am trying to download a file I have uploaded to an image field in my MS-SQL database. The problem is that when I try to open the file it just says System.Byte[] instead of containing the actual content.
UploadFiles is my class which contains the filename, id, filedata etc.
public void DownloadUploadedFile(Page sender, UploadFiles uf)
{
sender.Response.Clear();
sender.Response.ContentType = uf.FileType;
sender.Response.AddHeader("Content-Disposition",
"attachment; filename=" + uf.FileName);
sender.Response.BinaryWrite(uf.FileData); // the binary data
sender.Response.End();
}
Here I retrieve the data from my database:
while (reader.Read())
{
UploadFiles uf = new UploadFiles();
uf.FileData = encoding.GetBytes(reader["filedata"].ToString());
uf.FileName = reader["name"].ToString();
uf.FileType = reader["filetype"].ToString();
uf.FileId = Convert.ToInt32(reader["id"]);
return uf;
}
The
uf.FileData = encoding.GetBytes(reader["filedata"].ToString());
should be
uf.FileData = (byte[])reader["filedata"];
The data returned is a byte array, and you call ToString() on the byte array, which just defaults to returning the class name (system.byte[]) - which you then convert to a byte array. It should just be cast right away