我以前做的项目都是把上传的问题保存到文件夹,需要文件的时候,把服务器的文件下载到本地就行了,现在根据需要,服务器不保留文件了,而是把所有的文件转换为二进制的方式全部存入数据库,虽然效率低下,没办法啊,需求如此!特别向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#的

解决方案 »

  1.   

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

  2.   

    如梦真的是机器Response里面也是使用了,文件流操作,输出后发送给客户端。本地就像如梦那样做。闲暇时光不想浪费吗?用《Csdn收音机》找个话题聊聊技术吧!
      

  3.   

    byte[](dr[])//将图片读出来转换成byte数组
    Response.BindWirter();
      

  4.   


    这个应该是把文件转换成二进制吧?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>");
            }
        } 原文参考
      

  5.   

    //以字符流的形式下载文件
                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();
      

  6.   


    //二进制与图片互转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
      

  7.   

     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();
            }
      

  8.   

    ls们是不是想太多了?
    File.WriteAllBytes