Showing posts with label Upload picture type images. Show all posts
Showing posts with label Upload picture type images. Show all posts

Monday, July 20, 2009

ASP.NET code for Upload picture to database and display image (type images)

เมื่อฐานข้อมูลเดิม ของระบบเดิม ใช้การเก็บรูปภาพเป็นประเภท images ไม่ได้เก็บรูปไว้ในพาธ โฟลเดอร์อย่างที่เคยทำ

code นี้รันบน
- 3.5 Framework
- visual studio 2008
- sql server management studio express


โดยใช้ fileupload control ถ้าผิดพลาดหรือมีอะไรที่ควรแก้ไขช่วยแนะนำด้วยค่ะ
เพราะดูมาจากหลายที่ บางโค๊ดก็ไม่สามารถใช้ได้เหมือนตัวอย่าง ก็มิกซ์แอนแมทซ์ได้อย่างที่เห็น

เป็นระบบการเพิ่มข่าวสารสาระความรู้

ส่วนของ code (ไม่ได้ก็อปมาทั้งหน้า เอาเฉพาะที่ำสำคัญ) :



'ที่ต้อง import เข้ามา

Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.Configuration
Imports System.IO
Imports System.Web.UI.Page


Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("intranetConnectionString").ConnectionString)

'ฟังก์ชั่น replace ตัวอักษรที่ไม่ต้องการ
Function replacetext(ByVal str As String) As String
str = Trim(str).Replace("'", " ")
Return str
End Function


'เหตุการณ์เมื่อกดปุ่ม upload

Protected Sub btnaddnews_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnaddnews.Click
Dim stradd As String
Dim maxid As Integer

'ไฟล์อัพโหลด
If Not FileUpload1.PostedFile Is Nothing And FileUpload1.PostedFile.ContentLength > 0 Then

Dim fn As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)
Dim fs As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.ContentLength)
Dim ft As String = System.IO.Path.GetFileName(FileUpload1.PostedFile.ContentType)

Dim ft4add As String
Select Case Trim(ft)
Case "gif"
ft4add = "image/gif"
Case "pjpeg", "jpeg", "jpe", "jpg"
ft4add = "image/jpeg"
Case "png"
ft4add = "image/png"
Case Else
lblneedfile.Text = "ชนิดของรูปภาพไม่ถูกต้อง"
Exit Sub
End Select

Dim da As New SqlDataAdapter("select max(id_know) as maxx from tblKnowledge", conn)
Dim dt As New DataTable
da.Fill(dt)
If dt.Rows.Count <> 0 Then
maxid = CInt(dt.Rows(0).Item("maxx")) + 1
Else
maxid = 0
End If

stradd = "INSERT INTO tblKnowledge(id_know,FileName,FileSize,FileData,ContentType,topic,detail1,detail2,detail3,detail4,detail5,dateadd,addby,foot)"
stradd += " VALUES('" & maxid & "','" & fn & "','" & fs & "', @filedata ,'" & ft4add & "','" & replacetext(txttopic.Text) & "','" & replacetext(txtdetail1.Text) & "','" & replacetext(txtdetail2.Text) & "','" & replacetext(txtdetail3.Text) & "','" & replacetext(txtdetail4.Text) & "','" & replacetext(txtdetail5.Text) & "','" & Date.Now & "','" & CStr(Session("adminUser")) & "','" & replacetext(txtfrom.Text) & "')"

Dim myCommand As New SqlCommand(stradd, conn)
Dim imageBytes(System.IO.Path.GetFileName(FileUpload1.PostedFile.InputStream.Length)) As Byte
System.IO.Path.GetFileName(FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length))
myCommand.Parameters.AddWithValue("@filedata", imageBytes)
conn.Open()
myCommand.ExecuteNonQuery()
conn.Close()
Response.Redirect("index.aspx")
End If
End Sub


thank a lot :
ลองดูตัวอย่างจากเว็บนี้ก็ได้ค่ะ
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx

ส่วนการแสดงรูปจะใช้วิธีสร้างไฟล์ ขึ้นมา 1 ไฟล์ .aspx นี่แหละ
แล้วใช้ image control ส่งค่าไปตาม ImageUrl

โค๊ดแสดงรูปแบบ image

'ไฟล์แสดงรูป สมมุติว่าชื่อ formShowPic.aspx

If Not Page.IsPostBack Then
Dim cashowpic As New SqlDataAdapter("select FileData,ContentType from tblKnowledge where id_know='" & Trim(Request.QueryString("idpic")) & "'", conn)
Dim dtshowpic As New DataTable
cashowpic.Fill(dtshowpic)

If dtshowpic.Rows.Count <> 0 Then
If Not IsDBNull(dtshowpic.Rows(0).Item("FileData")) Then
Dim dataFromDtb As Byte() = DirectCast(dtshowpic.Rows(0).Item("FileData"), Byte())
Response.Clear()
Response.ContentType = Trim(dtshowpic.Rows(0).Item("ContentType").ToString)
Response.BinaryWrite(dataFromDtb)
Response.End()
End If
End If
End If




'เมื่อเวลาเราจะแสดงรูปไว้ตรงจุดใหนก็อ้าง image สมมุติว่า ใช่้ image ชื่อว่า im1 ใน code behine ก็เรียกดังนี้


im1.ImageUrl = "formShowPic.aspx?idpic=" & Trim(dtedit.Rows(0).Item("id_know").ToString)