468,505 Members | 1,790 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,505 developers. It's quick & easy.

Store uploaded image name into database

2
Hello all, im very newbie in asp code and now im having a project which needed to use ASP code, as for now i get the upload code from freaspupload website while it only upload to folder and im not sure how to store the image name into my database so later on i able to retrieve it in my website. The coding is as below.

Expand|Select|Wrap|Line Numbers
  1. <%@ Language=VBScript %>
  2.  
  3. <%
  4. option explicit
  5. Response.Expires = -1
  6. Server.ScriptTimeout = 600
  7.  
  8.  
  9. %>
  10. [bad html removed] #include file="freeaspupload.asp" -->
  11. [bad html removed] #include file="dbconnection.asp" -->
  12. <%
  13.  
  14.  
  15. ' ****************************************************
  16. ' Change the value of the variable below to the pathname
  17. ' of a directory with write permissions, for example "C:\Inetpub\wwwroot"
  18. Dim uploadsDirVar
  19. uploadsDirVar = "c:\inetpub\wwwroot\mmutrolley\upload"
  20. ' ****************************************************
  21.  
  22. ' Note: this file uploadTester.asp is just an example to demonstrate
  23. ' the capabilities of the freeASPUpload.asp class. There are no plans
  24. ' to add any new features to uploadTester.asp itself. Feel free to add
  25. ' your own code. If you are building a content management system, you
  26. ' may also want to consider this script: http://www.webfilebrowser.com/
  27.  
  28. function OutputForm()
  29. %>
  30. <form name="frmSend" method="POST" enctype="multipart/form-data" action="uploadTester.asp" onSubmit="return onSubmitForm();">
  31. <B>File names:</B><br>
  32. File : <input name="attach1" type="file" size=35><br>
  33. <br>
  34.  
  35. <input style="margin-top:4" type=submit value="Upload">
  36. </form>
  37. <%
  38. end function
  39.  
  40. function TestEnvironment()
  41. Dim fso, fileName, testFile, streamTest
  42. TestEnvironment = ""
  43. Set fso = Server.CreateObject("Scripting.FileSystemObject")
  44. if not fso.FolderExists(uploadsDirVar) then
  45. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not exist.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
  46. exit function
  47. end if
  48. fileName = uploadsDirVar & "\test.txt"
  49. on error resume next
  50. Set testFile = fso.CreateTextFile(fileName, true)
  51. If Err.Number<>0 then
  52. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have write permissions.</B><br>The value of your uploadsDirVar is incorrect. Open uploadTester.asp in an editor and change the value of uploadsDirVar to the pathname of a directory with write permissions."
  53. exit function
  54. end if
  55. Err.Clear
  56. testFile.Close
  57. fso.DeleteFile(fileName)
  58. If Err.Number<>0 then
  59. TestEnvironment = "<B>Folder " & uploadsDirVar & " does not have delete permissions</B>, although it does have write permissions.<br>Change the permissions for IUSR_<I>computername</I> on this folder."
  60. exit function
  61. end if
  62. Err.Clear
  63. Set streamTest = Server.CreateObject("ADODB.Stream")
  64. If Err.Number<>0 then
  65. TestEnvironment = "<B>The ADODB object <I>Stream</I> is not available in your server.</B><br>Check the Requirements page for information about upgrading your ADODB libraries."
  66. exit function
  67. end if
  68. Set streamTest = Nothing
  69. end function
  70.  
  71. function SaveFiles
  72. Dim Upload, fileName, fileSize, ks, i, fileKey
  73.  
  74. Set Upload = New FreeASPUpload
  75. Upload.Save(uploadsDirVar)
  76.  
  77. ' If something fails inside the script, but the exception is handled
  78. If Err.Number<>0 then Exit function
  79.  
  80. SaveFiles = ""
  81. ks = Upload.UploadedFiles.keys
  82. if (UBound(ks) <> -1) then
  83. SaveFiles = "<B>Files uploaded:</B> "
  84. for each fileKey in Upload.UploadedFiles.keys
  85. SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
  86. next
  87. else
  88. SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
  89. end if
  90. end function
  91. %>
  92.  
  93. <HTML>
  94. <HEAD>
  95. <TITLE>Test Free ASP Upload 2.0</TITLE>
  96. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  97. <style>
  98. BODY {background-color: white;font-family:arial; font-size:12}
  99. </style>
  100. <script>
  101. function onSubmitForm() {
  102. var formDOMObj = document.frmSend;
  103. if (formDOMObj.attach1.value == "" && formDOMObj.attach2.value == "" && formDOMObj.attach3.value == "" && formDOMObj.attach4.value == "" )
  104. alert("Please press the Browse button and pick a file.")
  105. else
  106. return true;
  107. return false;
  108. }
  109. </script>
  110.  
  111. </HEAD>
  112.  
  113. <BODY>
  114.  
  115. <br><br>
  116. <div style="border-bottom: #A91905 2px solid;font-size:16">Upload files to your server</div>
  117. <%
  118. Dim diagnostics
  119. if Request.ServerVariables("REQUEST_METHOD") <> "POST" then
  120. diagnostics = TestEnvironment()
  121. if diagnostics<>"" then
  122. response.write "<div style=""margin-left:20; margin-top:30; margin-right:30; margin-bottom:30;"">"
  123. response.write diagnostics
  124. response.write "<p>After you correct this problem, reload the page."
  125. response.write "</div>"
  126. else
  127. response.write "<div style=""margin-left:150"">"
  128. OutputForm()
  129. response.write "</div>"
  130. end if
  131. else
  132. response.write "<div style=""margin-left:150"">"
  133. OutputForm()
  134. response.write SaveFiles()
  135. response.write "<br><br></div>"
  136. end if
  137.  
  138. %>
  139.  
  140.  
  141.  
  142.  
  143. </BODY>
  144. </HTML>
Apr 4 '10 #1
4 3487
jhardman
3,405 Expert 2GB
Near the bottom of the page, there is a line that says "response.write savefiles()" if you want to store the file name in the db, you should that value in the db. Do you know how to update dbs in general? If so, the line might look something like this:
Expand|Select|Wrap|Line Numbers
  1. objRS("filePath") = saveFiles()
If you need help updating your db, please ask.

Jared
Apr 5 '10 #2
k1d0
2
Thanks alot for your reply...update database in normal ASP coding im ok, but for this VBscript im not sure,i try a few time and fail.Currenly i have a dbconnection.asp to connect database, the code for it is

Expand|Select|Wrap|Line Numbers
  1. <%
  2.  
  3. dim connDB
  4. Set connDB = Server.CreateObject("ADODB.Connection")  
  5. connDB.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath("mmutrolley.mdb") & ";Persist Security Info=False"
  6.  
  7. %>
  8.  
can you help me abit in updating? Really thanks alot..
Apr 5 '10 #3
jhardman
3,405 Expert 2GB
@k1d0
sorry for the delay, I thought I had replied to you earlier. Try this:
Expand|Select|Wrap|Line Numbers
  1. dim connDB, objRS
  2. Set connDB = Server.CreateObject("ADODB.Connection")  
  3. connDB.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.MapPath("mmutrolley.mdb") & ";Persist Security Info=False"
  4. set objRS = server.createobject("adodb.recordset")
  5. objRS.open "SELECT * FROM myTable", connDB, adopendynamic, adlockoptimistic
  6.  
  7. objRS.addNew()
  8. objRS("filePath") = saveFiles()
  9. objRS.update
  10.  
Just a couple things to note:
1- adopendynamic are constants, you might need to set those, they should be 3 and 2 I believe, but you can look them up to make sure
2- between addNew and update you can set any field value on that row of the table. Does this make sense?

Jared
Apr 21 '10 #4
Prathap
37
Solved
Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Imports System.Data
  3. Imports System.IO
  4.  
  5.  Public Class Form1
  6.     Dim str As String = "Data Source=NET3\SQLEXPRESS;Initial Catalog=RestPos;Persist Security Info=True;User ID=sa;Password=password"
  7.     Dim con As New SqlClient.SqlConnection
  8.  
  9.     'To open an image from computer
  10.     '-------------------------------
  11.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  12.         OpenFileDialog1.Title = "Please select a file"
  13.         OpenFileDialog1.InitialDirectory = "c:temp"
  14.         OpenFileDialog1.ShowDialog()
  15.         TextBox2.Text = OpenFileDialog1.FileName.ToString  '--->To Show the file path in textbox2
  16.         PictureBox1.ImageLocation = TextBox2.Text  '--->To show selected image in picturebox
  17.     End sub
  18.  
  19.    'To insert selected image into database
  20.    'The datatype of the column in table to store image should be <image>  
  21.    '--------------------------------------------------------------------
  22.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  23.         con.ConnectionString = str
  24.         Dim ms As New IO.MemoryStream()
  25.         PictureBox1.Image.Save(ms, PictureBox1.Image.RawFormat)
  26.         Dim arrimage() As Byte = ms.GetBuffer
  27.         Dim cmd As New SqlCommand("insert into image (Emp_Image)values(@picture)", con)
  28.         cmd.Parameters.Add(New SqlParameter("@Picture", SqlDbType.Image)).Value = arrimage
  29.         con.Open()
  30.         cmd.ExecuteNonQuery()
  31.         con.Close()
  32.     End Sub
  33.  
  34.     'We have successfully inserted the image into database.
  35.     'Now we want to Retrieve the image from database.
  36.     '-------------------------------------------------
  37.     Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  38.         Dim stream As New IO.MemoryStream()
  39.         con.Open()
  40.         Dim command As New SqlCommand("select Emp_Image from Image where Emp_Id='" + TextBox3.Text + "'", con) '--->You can give Emp_id instead of Textbox value.
  41.         Dim image As Byte() = DirectCast(command.ExecuteScalar(), Byte())
  42.         stream.Write(image, 0, image.Length)
  43.         con.Close()
  44.         Dim bitmap As New Bitmap(stream)
  45.         PictureBox2.Image = bitmap '--->I have used another picturebox to display image from database.
  46.     End Sub
  47.     'Thats all, You can place a linklabel below the picturebox if you to change photo and update it in database.
Regards,
Prathap
Jan 6 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Satish Appasani | last post: by
5 posts views Thread by IkBenHet | last post: by
8 posts views Thread by ctiggerf | last post: by
reply views Thread by harshad | last post: by
9 posts views Thread by sejal17 | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.