当前位置:编程学习 > C#/ASP.NET >>

asp.net File,FileStream,byte应用转换

asp教程.net file,filestream,byte应用转换

file>>>filestream (2种方式)
方式1:
filestream fs1 = file.open ( @"d:集团通知.jpg" , filemode.open );
方式2:
filestream fs2 = new filestream ( @"d:通知.jpg" , filemode.open );

filestream>>>byte[]
byte [ ] b1 = new byte [ fs1.length ];
fs1.read ( b1 , 0 , b1.length );

把文件保存到数据库教程中一般是file>>>filestream>>>byte[]

byte[]>>>file(2种方式)
假设byte[ ] b1里面有数据
方式1:
file.writeallbytes ( @"d:通知b2.jpg" , b1 );
方式2:
filestream fs = new filestream ( @"d:通知b_2.jpg" , filemode.openorcreate , fileaccess.write );
fs.write ( b1 , 0 , b1.length );
fs.close ( );

filestream>>>filestream
有时需要把文件流写到ftp的请求流中,实际上是利用byte[]做了一个缓冲区

stream requeststream = uploadrequest.getrequeststream ( );
filestream filestream =  file.open ( @"d:abc.txt" , filemode.open );

byte [ ] buffer = new byte [ 1024 ];
int bytesread;
while ( true )
{
bytesread = filestream.read ( buffer , 0 , buffer.length );
if ( bytesread == 0 )
break;
requeststream.write ( buffer , 0 , bytesread );
}

转换到filestream byte() array throws outofmemoryexception

'open the filestream data file for writing
      dim fs as new sqlfilestream(filepath, txcontext, fileaccess.write)

      'open the source file for reading
      dim localfile as new filestream("c:tempmicrosoftmouse.jpg",
                      filemode.open,
                      fileaccess.read)

      'start transferring data from the source file to filestream data file
      dim bw as new binarywriter(fs)
      const buffersize as integer = 4096
      dim buffer as byte() = new byte(buffersize) {}
      dim bytes as integer = localfile.read(buffer, 0, buffersize)

      while bytes > 0
        bw.write(buffer, 0, bytes)
        bw.flush()
        bytes = localfile.read(buffer, 0, buffersize)
      end while

      'close the files
      bw.close()
      localfile.close()
      fs.close()

 

补充:asp.net教程,基础入门
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,