从数据库读取文件
我正在尝试下载已上传到 MS-SQL 数据库中的图像字段的文件。问题是,当我尝试打开文件时,它只是显示 System.Byte[] 而不是包含实际内容。
UploadFiles 是我的类,其中包含文件名、id、文件数据等。
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();
}
这里我从数据库中检索数据:
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
返回的数据应该
。
是一个字节数组,您对字节数组调用 ToString(),它默认返回类名 (system.byte[]) - 然后将其转换为字节数组 应该立即施放
The
should be
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
尝试 uf.FileName.ToString(),否则您将获得对象类型,而不是 FileName 属性文本。
Try uf.FileName.ToString(), otherwise you're getting the object type, not the FileName property text.