Thoughts Caused by stream. Position

Source: Internet
Author: User
The file upload feature in Web development has always been a tough issue, especially when developing Ajax websites, faced with complex page elements, the implementation of the new file upload function without refreshing the page becomes more complicated. I am in workshop. Generally, the server processes the files to be uploaded.

Private List < Imageentity > Getuploadimages ()
{
List < Imageentity > Images =   New List < Imageentity > ();
Httpfilecollection files = Request. files;
Int Filelen;
Imageentity Image =   Null ;

If (Files ! =   Null   && Files. Count >   0 )
{
For ( Int I =   0 ; I < Files. Count; I ++ )
{
If (Files [I]. filename. Length ! =   0 )
{
Try
{
Filelen = Files [I]. contentlength;
Byte [] bytes =   New Byte [filelen];
Stream. Read (bytes, 0 , Filelen );

Image =   New Imageentity ();
Image. Type = Files [I]. contenttype;
Image. imageblob = Bytes;
Image. Title = Path. getfilename (files [I]. filename );
Images. Add (image );
}
Catch {}
}
}
}

ReturnImages;
}

This is an example of obtaining the Image File Uploaded by the client.CodeThe imageentity is an image object class. You can store images in the database (or on the server disk) based on the information described in this object class ).

Public   Class Imageentity
{
Public Imageentity ()
{
}

PublicImageentity (StringTitle, byte [] imageblob,StringType)
{
Title=Title;
Imageblob=Imageblob;
Type=Type;
}

Public   String Title { Get ; Set ;}
Public   String Type { Get ; Set ;}
Public Byte [] imageblob { Get ; Set ;}
}

This is no problem! When uploading an image, the page is submitted to the server through the POST method. The server constructs a byte array of the same size as the image, and obtains a system through the inputstream attribute of httppostedfile. io. stream object, and then use the read method of this object to read the image data stream to the byte array. In this process, the page needs to be returned, and the stream object will be re-constructed when the next user uploads an image, and then the byte array will be re-filled.

However, during a moss development, I accidentally found that there was no problem when I used this method to upload images to the database. When I read images from the database, a Red Cross was displayed, indicates that the image is unavailable or the image fails to be loaded. After carefully checking the data in the database, except that the field storing the binary data of the image is displayed as <binary data>, the data of other fields is normal and no exception is found. I debugged it.ProgramNo exception is thrown during Image Upload and storage, and everything goes smoothly. However, the page cannot load images normally when reading images.

At the beginning, I felt this problem was very strange. It seems that the problem was that the binary data of the image was obtained incorrectly or incomplete during image uploading. I debugged and tracked the code again, it is found that the values in the byte array are always 0 during the upload process, even when the stream. this is also true after the read method is filled with data. Why? Do I use the wrong object? I checked msdn and found that the above example basically obtained the file to be uploaded using this method and passed stream. when the read method is used to fill the byte array, the sample code of msdn will certainly not be wrong. Where is the error? I started to get confused...

I searched Google carefully. One of my friends gave me a helpful help. He suggested using stream. before filling the byte array with the read method, Judge stream. whether the position value is 0. If it is not 0, set it to 0 first, and then fill in the byte array. However, I first thought that the stream object constructed every time the page uploads a file is new (because the page will be PostBack back), and I didn't cache the stream object anywhere in the program, since the stream object is new, the value of the position attribute must be 0. I tried the method introduced by this friend with a skeptical attitude, and it really worked. It seems that the stream object is actually cached somewhere, or the stream object is not completely released after the previous file upload. So I modified the above method to get the uploaded image.

Private List < Imageentity > Getuploadimages ()
{
List < Imageentity > Images =   New List < Imageentity > ();
Httpfilecollection files = Request. files;
Int Filelen;
Imageentity Image =   Null ;

If (Files ! =   Null   && Files. Count >   0 )
{
For ( Int I =   0 ; I < Files. Count; I ++ )
{
If (Files [I]. filename. Length ! =   0 )
{
Try
{
Filelen = Files [I]. contentlength;
Byte [] bytes =   New Byte [filelen];
Using (Stream stream = Files [I]. inputstream)
{
Stream. Position =   0 ;
Stream. Read (bytes, 0 , Filelen );
}

Image =   New Imageentity ();
Image. Type = Files [I]. contenttype;
Image. imageblob = Bytes;
Image. Title = Path. getfilename (files [I]. filename );
Images. Add (image );
}
Catch {}
}
}
}

ReturnImages;
}

Because I developed it on the moss platform, it is different from a common ASP. NET project. It may be affected by many Moss, such as the cache mechanism. However, if an object like stream is involved, it is best to release it immediately after use, because even if stream is not cached, it may also occupy the memory for a long time and consume a lot of server resources. We recommend that you use stream in the using statement and reset position to 0 before filling in the byte array using the read method. This ensures that the byte array is correctly filled and the file data is obtained correctly.

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.