Asp tutorial. net file, filestream, byte application conversion
File >>> filestream (two methods)
Method 1:
Filestream fs1 = file. open (@ "d: group notification .jpg", filemode. open );
Method 2:
Filestream fs2 = new filestream (@ "d: notification .jpg", filemode. open );
Filestream >>> byte []
Byte [] b1 = new byte [fs1.length];
Fs1.read (b1, 0, b1.length );
In the tutorial of saving a file to a database, it is generally file >>> filestream >>> byte [].
Byte [] >>> file (two methods)
Assume that byte [] b1 contains data
Method 1:
File. writeallbytes (@ "d: notification b2.jpg", b1 );
Method 2:
Filestream fs = new filestream (@ "d: notification B _2.jpg", filemode. openorcreate, fileaccess. write );
Fs. write (b1, 0, b1.length );
Fs. close ();
Filestream >>> filestream
Sometimes you need to write the file stream to the ftp request stream. In fact, byte [] is used as a buffer.
Stream requeststream = uploadrequest. getrequeststream ();
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 );
}
Convert to 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 ()