I'm uploading a image file, for that I have used <input type="file" name="browse_file" accept="image/*"/>
in my form.
I have a table called covers
which has a column called img
with data type image
.
This is what my html looks like
HTML
<table border="1" cellpadding="5" cellspacing="0" width="100%">
<tr>
<td width="20%"><b>Choose Cover:</b></td>
<td>
<input type="file" name="browse_file" accept="image/*"/>
<%
session("file_name") = request.Form("browse_file")
%>
</td>
</tr>
<tr>
<td><strong>Cover Name:</strong></td>
<td><input type="text" name="cover_name" value=""></td>
</tr>
<tr>
<td><strong>Choose Category:</strong></td>
<td>
<select name="disp_cat" onchange="return refresh_cat();">
<option selected="selected" value="0">Choose Category...</option>
<%
sql = "SELECT * from categories"
rs.open sql, con, 1, 2
do while not rs.eof
%>
<% if rs("name") = request.Form("disp_cat") then%>
<option value="<%=rs("name")%>" selected="selected"><%=rs("name")%></option>
<%else%>
<option value="<%=rs("name")%>"><%=rs("name")%></option>
<%end if
rs.movenext
loop
rs.close
%>
</select>
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="Save" name="save_cover" onclick="return show_alert2();"/>
<input type="submit" value="Cancel" name="cancel" onclick="return go_back();"/>
</td>
</tr>
<%
if request.Form("save_cover") <> "" then
sql = "SELECT * from covers"
rs.open sql, con, 1, 2
rs.addnew
rs("c_name") = request.Form("cover_name")
rs("category") = request.Form("disp_cat")
rs("img") = request.Form("browse_file")
rs("date_upl") = date()
rs.update
response.Write("<script language='javascript'>{update1();}</script>")
rs.close
end if
%>
</table>
Connection to database is defined in a separate file called database.asp
. The connection is working because all the columns like c_name
, category
, date_upl
are getting updated in the database table covers
except for the img
column.
It's not taking up the image path. Is rs("img") = request.Form("browse_file")
a wrong asp recordset to update database column?
If you're using the SQL Image
Datataype, that's a binary field
to store the image itself, not the path - if you just want to store the path you would use a varchar
data type.
If you want to store the image in the database (as binary) then the easiest way to do it is to save the file to disk (using FreeASPUpload etc), then tell SQL to load that file into the binary column, for example:
INSERT INTO covers(img)
SELECT * FROM
OPENROWSET(BULK N'c:\uplaods\img0001.jpg', SINGLE_BLOB) AS import
Or, if you want to insert the form data then upload the image into SQL afterwards:
UPDATE covers SET [img] = (SELECT NewImage.* from Openrowset(Bulk 'c:\uplaods\img0001.jpg', Single_Blob) NewImage) where Id = 10
Personally though - i dont store images in SQL, i keep them on disk and just store the filename in SQL, eg:
function SaveFiles
Dim Upload, fileName, fileSize, ks, i, fileKey
Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)
If Err.Number <> 0 then Exit function
SaveFiles = ""
ks = Upload.UploadedFiles.keys
if (UBound(ks) <> -1) then
SaveFiles = "<B>The following was uploaded:</B> "
for each fileKey in Upload.UploadedFiles.keys
SaveFiles = SaveFiles & Upload.UploadedFiles(fileKey).FileName & " (" & Upload.UploadedFiles(fileKey).Length & "B) "
Dim cover_name, disp_cat
cover_name = Upload.Form("cover_name")
disp_cat = Upload.Form("disp_cat")
'#### Double Up any quotes (very lazy - use paramaterised SQL instead)
cover_name = replace(cover_name, "'", "''")
disp_cat = replace(disp_cat, "'", "''")
'Dump file information into database
SQLnewFile = "INSERT into covers (File_Filename, cover_name, disp_cat) " & _
"VALUES ('" & replace(Upload.UploadedFiles(fileKey).FileName,"'","''") & "', '" & cover_name & "', '" & disp_cat & "')"
db.Execute(SQLnewFile)
next
else
SaveFiles = "The file name specified in the upload form does not correspond to a valid file in the system."
end if
end function
Edit 1: In Response To Your Comments
I dont usually write peoples code for them but you seem to be struggling to grasp the basics here, no, you would not edit your .inc file. The bit of code you would edit is the code where you check for the post and insert the data, for example:
if request.Form("save_cover") <> "" then
Dim Upload, fileName, ks, fileKey, uploadsDirVar
uploadsDirVar = "c:\Uploads\"
Set Upload = New FreeASPUpload
Upload.Save(uploadsDirVar)
ks = Upload.UploadedFiles.keys
if (UBound(ks) <> -1) then
for each fileKey in Upload.UploadedFiles.keys
Dim cover_name, disp_cat
cover_name = replace(Upload.Form("cover_name"), "'", "''")
disp_cat = replace(Upload.Form("disp_cat"), "'", "''")
SqlNewCover = "INSERT into covers (cover_name, disp_cat, FileName, img) " & _
"VALUES ('" & cover_name & "', '" & disp_cat & "', '" & replace(Upload.UploadedFiles(fileKey).FileName,"'","''") & "', (SELECT BulkColumn FROM OPENROWSET( Bulk '" & uploadsDirVar & replace(Upload.UploadedFiles(fileKey).FileName,"'","''") & "', SINGLE_BLOB) AS BLOB))"
db.Execute(SQLnewFile)
next
end if
end if