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

作者:袖梨 2022-06-25

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

 

相关文章

精彩推荐