我以前做的项目都是把上传的问题保存到文件夹,需要文件的时候,把服务器的文件下载到本地就行了,现在根据需要,服务器不保留文件了,而是把所有的文件转换为二进制的方式全部存入数据库,虽然效率低下,没办法啊,需求如此!特别向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#的
这里是我保存文件转为二进制流存入数据库的方法 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#的
解决方案 »
- 在线考试系统如何实现一个带图的题目
- 为什么全局Cookie赋值后要重新激发页面才能刷新值?
- 关于绑定了GridView控件后怎么截取字符串!
- Asp.net转换word到PDF的问题?
- 祝大家周末放松!问个问题,嘻嘻:web窗口关闭时触发的是什么事件?
- DataGird分页怎么有问题???
- 高分求救:大文件上传(不限制文件大小,带进度条,用vb)
- 我有两个问题,麻烦各位高手看看!!!!!
- DATAGRID难题(添加、删除、修改纪录到dataset中的问题,不实际提交到数据库中)!请高手指点!(200分,分2次给!申明一定给分)
- 请教一个关于链接的问题
- UpdatePanel如何在内容页将控件注册为异步回发
- ASP.NET MVC 如何将前台后台分开?
Response.BindWirter();
这个应该是把文件转换成二进制吧?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>");
}
} 原文参考
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();
//二进制与图片互转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
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();
}
File.WriteAllBytes