Uploading image and image path to a database
I want to upload image path to a database and image to a folder. How can I do that?
Answer:
We assume that you have inserted Pure ASP Upload behavior into your ASP page. While inserting it, please make sure that you have set "Store" setting to "Full Path".
Insert a recordset and select your database where you want to store the image path.
Then from the Server Behaviors tab insert "Insert Record" behavior and select the appropriate table and column where you want to store your image path. Make sure "Submit as:" is set to "Text".
Here is an example code:
<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%>
<!--#include file="Connections/MyConnection.asp" -->
<!--#include file="ScriptLibrary/incPU3Class.asp" -->
<!--#include file="ScriptLibrary/incPU3Utils.asp" -->
<%
'*** Pure ASP File Upload 3.0.9
' Process form form1
Dim pau, DMX_uploadAction, UploadRequest, UploadQueryString, pau_thePath, pau_nameConflict, pau_saveWidth, pau_saveHeight
Set pau = new PureUpload
pau.ScriptLibrary = "ScriptLibrary"
pau.ConflictHandling = "over"
pau.StoreType = "path"
pau.UploadFolder = """images"""
pau.AllowedExtensions = "GIF,JPG,JPEG,BMP,PNG" ' "images"
pau.ProcessUpload
pau.SaveAll
%>
<%
Dim MM_editAction
MM_editAction = CStr(Request.ServerVariables("SCRIPT_NAME"))
If (UploadQueryString <> "") Then
MM_editAction = MM_editAction & "?" & Server.HTMLEncode(UploadQueryString)
End If
' boolean to abort record edit
Dim MM_abortEdit
MM_abortEdit = false
%>
<%
If (CStr(UploadFormRequest("MM_insert")) = "form1") Then
If (Not MM_abortEdit) Then
' execute the insert
Dim MM_editCmd
Set MM_editCmd = Server.CreateObject ("ADODB.Command")
MM_editCmd.ActiveConnection = MM_MyConnection_STRING
MM_editCmd.CommandText = "INSERT INTO Images (tblImages) VALUES (?)"
MM_editCmd.Prepared = true
MM_editCmd.Parameters.Append MM_editCmd.CreateParameter("param1", 202, 1, 255, UploadFormRequest("fileField")) ' adVarWChar
MM_editCmd.Execute
MM_editCmd.ActiveConnection.Close
End If
End If
%>
<%
Dim rsImages
Dim rsImages_cmd
Dim rsImages_numRows
Set rsImages_cmd = Server.CreateObject ("ADODB.Command")
rsImages_cmd.ActiveConnection = MM_MyConnection_STRING
rsImages_cmd.CommandText = "SELECT * FROM Images"
rsImages_cmd.Prepared = true
Set rsImages = rsImages_cmd.Execute
rsImages_numRows = 0
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8"" />
<title>Upload Images to a DB</title>
<script type="text/javascript"><%=pau.generateScriptCode()%></script>
<script src="../../ScriptLibrary/incPU3.js" type="text/javascript"></script>
</head>
<body>
<form action="<%=MM_editAction%>" method="post" enctype="multipart/form-data" name="form1" id="form1" onsubmit="<%=pau.submitCode()%>;return document.MM_returnValue">
<input name="fileField" type="file" id="fileField" onchange="<%=pau.validateCode()%>;return document.MM_returnValue" />
<input type="submit" name="button" id="button" value="Submit" />
<input type="hidden" name="MM_insert" value="form1" />
</form>
</body>
</html>
<%
rsImages.Close()
Set rsImages = Nothing
%>
NOTE: My recordset is named rsImages & my column that I am inserting image path to is named tblImages
DISCLAIMER:
This is extra complimentary content which purpose is to show
additional usage that is not part of the product, i.e. it suggests tips
for extending the functionality of the product by the users themselves.
DMXzone does not take responsibility to support the suggested
content to be fully compatible and working as intended and does not
provide support for extended functionality which is not included in the
current release of the extension.
It is highly recommended that only more advanced developers use it.
Comments
Be the first to write a comment
You must me logged in to write a comment.