Asp.net File, FileStream, byte application conversion

Source: Internet
Author: User

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

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.