473,287 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,287 software developers and data experts.

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 3675
jhardman
3,406 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,406 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

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

Similar topics

2
by: bissatch | last post by:
Hi, I am trying to write script that is run when a form is submitted. The form contains an image input field and when submitted, the image is uploaded, resized and added as binary information to...
0
by: Satish Appasani | last post by:
Hi: I have a ASP.NET form with Web layout which I've achieved using panels. In one of the tab I have a File control to upload Images. When I put a file in the file control and move to another...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
5
by: moondaddy | last post by:
I'm caching a dataset in an asp.net session variable to hold a user's data. one data item I need to store is an image the user uploaded. My problem is that I don't know how to get the image into...
2
by: Brad | last post by:
I have code which takes an image, uploaded from a web page, and saves it to a database. Now I want to always resize an uploaded image before it is saved to the database. My code to resize is...
4
by: RedHair | last post by:
I'd like to set up a file system for the ASP.NET 2.0 application to store user-uploaded files, since the members are more than 100,000 people, the basic requirements are as below: (1) The file...
8
by: ctiggerf | last post by:
I was hopeing someone could help me out here. Been stumped on this one all day. This function 1. Checks uploaded files. 2. Creates two resized images from each (a full size, and a...
0
by: harshad | last post by:
Dear All,Here I am facing problem to store image.I am trying to store byte array(image) in to session variable so at time of update I will got that byte array and I do my update. here i am given...
9
by: sejal17 | last post by:
hi all, To upload image i want to make an folder of username using which user is login and put image in that folder thanks
8
by: ahilar12 | last post by:
Hi experts, I have a form with many textboxes,listboxes in php.I have a edit button to edit the values in the form.once i click the edit button the existing values should be displayed so that...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.