ASP. NET (C #) Upload image (Binary)

Source: Internet
Author: User
This method is suitable for ASP. NET (C #) + Oracle
The field type in the Oracle table is blob.
# Region Upload File (Binary)
//
Static public byte [] filetobinary (fileupload hifile)
{
Try
{
If (hifile. hasfile)
{
If (isallowedextension (hifile ))
{
// Obtain the size of the uploaded file
Int filelen = hifile. postedfile. contentlength;

Byte [] filedata = new byte [filelen];

// Create an object for accessing the client to upload files
Httppostedfile HP = hifile. postedfile;

// Create a data stream object
Stream sr = hp. inputstream;

// Put the image data in the filedata array object instance. 0 indicates the starting position of the array pointer, and filelen indicates the Ending position of the pointer.
Sr. Read (filedata, 0, filelen );

// String id = "0 ";
// BLL. Picture. addpictures (ID, "name", convert. tostring (filedata), "url", "Description ");

Return filedata; // you only need to save the binary file stream to the database.

}
Else
{
// Response. write ("<SCRIPT upload Ge = 'javascript 'Type = 'text/JavaScript '> alert ('upload files in incorrect format, only jpg and GIF formats are allowed to be uploaded '); </SCRIPT> ");
Return NULL; // "the format of the uploaded file is incorrect. Only jpg and GIF files can be uploaded ";
}
}
Else
{
Return NULL; // "Select Upload File ";
// Response. Write ("<script language = 'javascript 'Type = 'text/JavaScript '> alert ('select upload file'); </SCRIPT> ");
}
}
Catch
{
Return NULL;
}
}
# Endregion

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.