Get the App
SLTechnology News&Howtos  ›  Development  › 

How to save a picture in a database after uploading a picture

Shulou Source: shulou.com Published: 2022-06-03 07:48:39 10月04日 Update

This article mainly explains the "tutorial on the method of using the database to save pictures after uploading pictures". The content of the explanation in the article is simple and clear, and it is easy to learn and understand. let's study and learn the tutorial on the method of using database to save pictures after uploading pictures.

The copy code is as follows:

HttpPostedFile UpFile = File1.PostedFile

Int FileLenght = UpFile.ContentLength

Decimal FileSize = FileLenght / 1024

If (FileLenght = = 0)

{

DIVShowGVError.Visible = true

LblShowGVError.Text = "Please choose to upload files"

Return

}

String strImageName = UpFile.FileName

String strImageType = strImageName.Substring (strImageName.LastIndexOf ("."). ToLower ()

If (strImageType! = ".jpg" & & strImageType! = ".jpeg" & & strImageType! = ".bmp" & & strImageType! = ".png")

{

DIVShowGVError.Visible = true

LblShowGVError.Text = "Please upload the film in the following file format: jpg,jpeg,bmp,png"

Return

}

If (FileSize > 2048)

{

DIVShowGVError.Visible = true

LblShowGVError.Text = "upload film cannot be larger than 2m"

Return

}

Else

{

Byte [] FileByteArray = new Byte [FileLenght]

Stream StreamObject = UpFile.InputStream

StreamObject.Read (FileByteArray, 0, FileLenght)

StrNewLeave = "insert into [Leave] ([ID], [EmployeeNo], [AgentID], [LeaveType], [StartDate], [EndDate], [Hours], [LeaveReason], [ReportTime], [ReportID], [ImageName], [ImageData])"

StrNewLeave + = "values (@ LeaveNo,@EmpNo,@AgentName,@LeaType,@StartDate,@EndDate,@Hours,@Reason,getdate (), @ Admin,@ImageName,@Image)"

SqlPara = new SqlParameter [] {

New SqlParameter (@ LeaveNo, strLeaNo)

New SqlParameter (@ EmpNo, strEmpNo)

New SqlParameter (@ AgentName, strAgentID)

New SqlParameter (@ LeaType, strLeaType)

New SqlParameter (@ StartDate, strDateSt)

New SqlParameter (@ EndDate, strDateEnd)

New SqlParameter (@ Hours, strHours)

New SqlParameter (@ Reason, strReason)

New SqlParameter (@ Admin, strAdmin)

New SqlParameter (@ ImageName, strImageName)

New SqlParameter (@ Image, FileByteArray)

}

StreamObject.Close ()

}

The copy code is as follows:

SQLHelper sqlH = new SQLHelper ()

String strID = context.Request ["ID"]

Byte [] MyData = new byte [0]

String str = "select [ImageData] from [Leave] where [ID] ='" + strID + ""

DataTable dt = sqlH.ExecuteQuery (str, CommandType.Text)

If (dt.Rows.Count > 0)

{

MyData = (byte []) dt.Rows [0] [0]

Int ArraySize = MyData.GetUpperBound (0)

Context.Response.OutputStream.Write (MyData, 0, ArraySize)

}

Thank you for your reading. the above is the content of the tutorial on the method of using the database to save pictures after uploading pictures. After the study of this article, I believe you have a deeper understanding of the method of using the database to save pictures after uploading pictures, and the specific use still needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Pictures tutorials data databases methods code learning content files that is ideas situations articles more formats knowledge knowledge points articles follow questions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Apple Docker Shulou Information NVidia Shulou Tech Info