asp.net如何把二进制流还原为文件?

qq22345111 2011-01-22 07:35:42
我以前做的项目都是把上传的问题保存到文件夹,需要文件的时候,把服务器的文件下载到本地就行了,现在根据

需要,服务器不保留文件了,而是把所有的文件转换为二进制的方式全部存入数据库,虽然效率低下,没办法啊,需求如此!

特别向CSDN的朋友们求助了!

这里是我保存文件转为二进制流存入数据库的方法

if (this.File1.PostedFile.FileName != "")
{
//得到提交的文件
Stream fileDataStream = this.File1.PostedFile.InputStream;

//得到文件大小
int fileLength = this.File1.PostedFile.ContentLength;

//创建数组
byte[] fileData = new byte[fileLength];

//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);

//获取上传文件的完整路径以及文件名
string FullName = this.File1.PostedFile.FileName.ToString();

//得到文件名字
string fileTitle = FullName.Substring(FullName.LastIndexOf("\\") + 1);

//得到文件类型
string fileType = FullName.Substring(FullName.LastIndexOf(".") + 1);
-------------------------插入数据库的操作就没必要贴了-----------------------------------------------------
}


Response.BinaryWrite这个方法也只是把二进制流文件输出到页面

求:把二进制还原为文件的代码!C#的
...全文
887 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq22345111 2011-01-23
  • 打赏
  • 举报
回复
我的错,想复杂了。可怜了我的200分
机器人 2011-01-22
  • 打赏
  • 举报
回复
ls们是不是想太多了?
File.WriteAllBytes 
happy664618843 2011-01-22
  • 打赏
  • 举报
回复
 string imagename = "";
try
{
con.Open();
SqlCommand com = new SqlCommand("select name from tb_17 where id="+DropDownList1.Text+"", con);
SqlDataReader dr = com.ExecuteReader();
dr.Read();
MemoryStream ms = new MemoryStream((Byte[])dr["name"]);
Bitmap image = new Bitmap(ms);
string filepath = Server.MapPath("Files/");
DirectoryInfo dir = new DirectoryInfo(filepath);
FileInfo[] filecount = dir.GetFiles();
int i = filecount.Length;
imagename = filepath + ((i + 1) + ".jpg");
image.Save(imagename);
dr.Close();
Image1.ImageUrl = "Files/" + ((i + 1) + ".jpg");
}
finally
{
con.Close();
}
子夜__ 2011-01-22
  • 打赏
  • 举报
回复
叶子 2011-01-22
  • 打赏
  • 举报
回复

//二进制与图片互转

Image aa = new Bitmap(@"E:\photo\tm.jpg");

System.IO.MemoryStream stream = new System.IO.MemoryStream();
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); formatter.Serialize(stream, aa); //将图像序列化成二进制流
stream.Position = 0;
Image bb = (Image)formatter.Deserialize(stream); //将二进制流序列成Image



wuyq11 2011-01-22
  • 打赏
  • 举报
回复
//以字符流的形式下载文件
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
子夜__ 2011-01-22
  • 打赏
  • 举报
回复
子夜__ 2011-01-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]

System.IO.File.WriteAllBytes("c:\\path\\filename", bytes);
[/Quote]

这个应该是把文件转换成二进制吧?

private void FileDown(string strPath)
{
System.IO.FileInfo file = new System.IO.FileInfo(strPath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.FullName, System.Text.Encoding.UTF8));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
else
{
ClientScript.RegisterStartupScript(GetType(), "", "<script language='javascript'>alert('文件不存在!');</script>");
}
}

原文参考
happy664618843 2011-01-22
  • 打赏
  • 举报
回复
byte[](dr[])//将图片读出来转换成byte数组
Response.BindWirter();
勤奋的大头怪 2011-01-22
  • 打赏
  • 举报
回复
看这像java语句。。。
boringame 2011-01-22
  • 打赏
  • 举报
回复
如梦真的是机器。。。

Response里面也是使用了,文件流操作,输出后发送给客户端。

本地就像如梦那样做。

闲暇时光不想浪费吗?用《Csdn收音机》找个话题聊聊技术吧!
wuyq11 2011-01-22
  • 打赏
  • 举报
回复
System.IO.File.WriteAllBytes("c:\\path\\filename", bytes);

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧