How to save a picture in a database after uploading a picture
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!