Upload multiple images

Hi,

The attached code allows to upload only one image.  I would like to be able to upload more than one image, for instance 6 images.  Please help me with this.
At the moment, If an image is not uploaded, an error appears.  How can I get the code to add a default name like 'default_image.gif' if an image is not selected.

There are other 4 asp files that go with this code, please let me know if they are needed.

thanks.
<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%> 
 
<html>
<head>
<title>DJ Skinner Lettings Add</title>
<LINK HREF="../style.css" TYPE="text/css" REL="stylesheet">
</head>
 
<body >
<div align="center">
  <table width="345" border="0" cellpadding="0" cellspacing="0">
          <form ACTION="lettings_add1.asp" METHOD="POST"  name="form1" encType="multipart/form-data">
    <tr> 
      <td width="125" height="19">&nbsp;</td>
      <td width="220">&nbsp;</td>
    </tr>
    <tr> 
      <td height="24" valign="top" bgcolor="#FFFFFF">Upload Image:</td>
      <td valign="top"> <input name="File1" type="file" id="File11" value=""></td>
    </tr>
    <tr> 
      <td height="24" colspan="2" valign="top"><div align="center"> 
          <input name="Submit" type="submit" value="Submit">
          &nbsp;&nbsp; </div></td>
    </tr>
</form>
  </table>
</div>
</body>
</html>
 
 
 
 
 
<!--#INCLUDE file="clsUpload.asp"-->
 
<%
Dim Upload
Dim Description
Dim FileName
Dim Folder
Set Upload = New clsUpload
 
FileName = Upload("File1").FileName
Folder = Server.MapPath("Uploads") & "\"
FileName = Upload.UniqueName(Folder, FileName)
Upload("File1").SaveAs Folder & FileName
set conn = createobject("adodb.connection")
 
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\121fina.mdb;")
sSQL = "INSERT INTO prop (FileName) VALUES ('"&FileName&"')"
conn.execute(sSQL)
  'please make sure you change these field names to exactly what they should be in the database
  conn.close 'clean up
set conn=nothing
Set Upload = Nothing
 
response.redirect("lettting_add_success.asp?str_h_no=" & h_no & "&" & "str_street=" & street)
 
%>

Open in new window

heymeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

hc0904pcdCommented:
Hi,
You need to put in a for-next loop, for x = 1 to number_of_images, upload & process it.
Have you worked with loops before?
0
hc0904pcdCommented:
In your first file, add some more input fields.
To keep it simple, lets name them File1, File2, File3 etc.
0
hc0904pcdCommented:
In your processing file, add this in

Set Upload = New clsUpload
NumberOfUploadFiles = 6

for x = 1 to NumberOfUploadFiles
   FileName = Upload("File" & x).FileName
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   set conn = createobject("adodb.connection")
   conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\121fina.mdb;")
   sSQL = "INSERT INTO prop (FileName) VALUES ('"&FileName&"')"
   conn.execute(sSQL)
   conn.close 'clean up
next

set conn=nothing
Set Upload = Nothing

and then test it, and let me know whether it works, and if not what error msg you get.


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

hc0904pcdCommented:
By the way, another option is to loop through the form elements and process them.
That way you don't need to have a preset number of input fields.
Once you get this sample above working, let me know whether you need to have a fixed or flexible number of uploaded images.
0
heymeAuthor Commented:
Hi,

Thanks for your response.

I have tried what you have shown, and the upload works fine.  thank you for that.
But, you have not shown how I can go about inserting the image names into the database.  at the moment, only the first one has the image name uploaded.

Also as mentioned above, if I do not wish to select an image, I would like by default to add a default image name:  like 'default_image.gif' if an image is not selected.

One other thing regarding uploading an image, if I upload a perfect square image like 150x 150 or 400 by 400, the image is uploaded fine, but if I upload an image that is 150x200, the image is squashed.  Please help me with this if you can.

Many thanks

thanks.
yasir
0
hc0904pcdCommented:
Hi yasir,
If the filename is going into the db the same each time, try this instead,

NumberOfUploadFiles = 6

for x = 1 to NumberOfUploadFiles
   Set Upload = New clsUpload
   FileName = Upload("File" & x).FileName
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   set conn = createobject("adodb.connection")
   conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\121fina.mdb;")
   sSQL = "INSERT INTO prop (FileName) VALUES ('"&FileName&"')"
   conn.execute(sSQL)
   conn.close 'clean up
   set conn=nothing
   Set Upload = Nothing
next

It's not the tidiest of code, but should do the trick...
0
hc0904pcdCommented:
To put in default values when an image is not selected, test for an empty value within the loop.
And don't do the upload command if the input field is empty.

ie
  FileName = Upload("File" & x).FileName
  if FileName = "" then
     FileName = "default_image.gif"
  else
      Folder = Server.MapPath("Uploads") & "\"
      FileName = Upload.UniqueName(Folder, FileName)
      Upload("File" & x).SaveAs Folder & FileName
  end if

0
hc0904pcdCommented:
About the image size.....

How are the uploaded images being used?

Are they displayed in a fixed-sized, in which case you want to resize them as they upload?
Or, do you want to grab the image dimensions when you display the image, and display it as it was loaded?
0
heymeAuthor Commented:
Hi,

I have tried what you have suggested and added the code below, i'm getting the following error:

ADODB.Stream error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

/admin/clsUpload.asp, line 182


with regards to the images, I would like to display them in a fixed-sized.

<!--#INCLUDE file="clsUpload.asp"-->
 
<%
Dim Upload
Dim Description
Dim FileName
Dim Folder
 
NumberOfUploadFiles = 3
 
for x = 1 to NumberOfUploadFiles
   Set Upload = New clsUpload
   FileName = Upload("File" & x).FileName
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   set conn = createobject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\kunden\homepages\16\d222578010\odbc\121fina.mdb;")
   sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES ('"&FileName&"', '"&FileName2&"','"&FileName3&"')"
   conn.execute(sSQL)
   conn.close 'clean up
   set conn=nothing
   Set Upload = Nothing
next
 
 
%>

Open in new window

0
hc0904pcdCommented:
Hi,
Try changing this

sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES ('"&FileName&"', '"&FileName2&"','"&FileName3&"')"

to this

sSQL = "INSERT INTO prop (FileName) VALUES ('"&FileName&"')"
0
hc0904pcdCommented:
I'm not sure how familiar you are with for next loops?
The code between for x = 1 to NumberOfUploadFile and next, is a loop.
It will run once where x = 1, the first file.
It will come back, and run again with x equal to 2
And then again with x = 3.

So you can't write all 3 values to the db in one go. Not within this loop.
Hope that makes sense?


0
heymeAuthor Commented:
I tried this code before:
sSQL = "INSERT INTO prop (FileName) VALUES ('"&FileName&"')"

yes, it does work in terms of upload the image and the image name, but how do I go about upload the rest of the images relating to the same record.

plus, the following error is displayed:
ADODB.Stream error '800a0bb9'
Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/admin/clsUpload.asp, line 182
0
hc0904pcdCommented:
The multiple images all need to be written to the one table record?
0
hc0904pcdCommented:
okay, lets change it slightly,


NumberOfUploadFiles = 3
sqlStr = ""
 
for x = 1 to NumberOfUploadFiles
   Set Upload = New clsUpload
   FileName = Upload("File" & x).FileName
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   sqlStr = sqlStr & FileName & ","
   Set Upload = Nothing
next

'now remove the trailing comma
if right(sqlStr,1) = "," then sqlStr = left(sqlStr,(len(sqlStr)-1))

set conn = createobject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\kunden\homepages\16\d222578010\odbc\121fina.mdb;")
sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES ('" & sqlStr & "' )"
conn.execute(sSQL)
conn.close 'clean up
set conn=nothing
0
heymeAuthor Commented:
How can I go about uploading and updating the database at the same time. rather than writting to the table manually.  I want to escape from doing manual data input.
0
hc0904pcdCommented:
regardnig the error msg,

ADODB.Stream error '800a0bb9' Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.  
/admin/clsUpload.asp, line 182

which line is 182 - can you copy it here?
thanks.
0
heymeAuthor Commented:
I've uploaded the file with the error.

thanks
clsUpload.txt
0
hc0904pcdCommented:
according to your file, line 182 is this
BinaryStream.Write(Request.BinaryRead(ChunkSize))

is that correct?
0
heymeAuthor Commented:
yes, that's correct
0
hc0904pcdCommented:
okay, lets move two lines out of the loop and test


NumberOfUploadFiles = 3
sqlStr = ""
Set Upload = New clsUpload
for x = 1 to NumberOfUploadFiles
   FileName = Upload("File" & x).FileName
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   sqlStr = sqlStr & FileName & ","
next
Set Upload = Nothing

'now remove the trailing comma
if right(sqlStr,1) = "," then sqlStr = left(sqlStr,(len(sqlStr)-1))

'write
set conn = createobject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\kunden\homepages\16\d222578010\odbc\121fina.mdb;")
sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES ('" & sqlStr & "' )"
conn.execute(sSQL)
conn.close 'clean up
set conn=nothing
0
heymeAuthor Commented:
now i'm getting this error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.
/admin/test1.asp, line 28

line 28 being the following line:
conn.execute(sSQL)

0
hc0904pcdCommented:
are the number of query values and destination fields the same?
what is the value of sSQL prior to execution?
0
heymeAuthor Commented:
value of sSQL:
INSERT INTO prop6 (FileName, FileName2, FileName3) VALUES ('6_p1[3].jpg,6_w2.gif,4_line.gif' )
0
heymeAuthor Commented:
the images are uploaded fine, its just this error.
0
hc0904pcdCommented:
Ah, I see the problem.
Do you see that the values string has 'around all of it' ?
Should be around each item.
0
hc0904pcdCommented:
change
sqlStr = sqlStr & FileName & ","
to  
sqlStr = sqlStr & "'" & FileName & "',"

and change
sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES ('" & sqlStr & "' )"
to
sSQL = "INSERT INTO prop (FileName, FileName2, FileName3) VALUES (" & sqlStr & " )"
0
heymeAuthor Commented:
Excellent, it works.
thank you very much for all your help.

I have one more request, sorry if I'm being cheeky.
I would like to be able to update the images.  I tried doing this before and I couldnt do it properly.
so that it will allow me to delete the existing image and upload the new images selected.

thanks for your help again.

0
hc0904pcdCommented:
Hi,
To update an image, do you want to update the physical image of an existing record?
0
heymeAuthor Commented:
Hi,

Yes, update any of the images of an existing record.

0
hc0904pcdCommented:
Okay, I think there's 2 ways of doing that.

1. You could upload the replacement image and use fso to edit filenames.
Find and delete the old version of the image.
The rename the newly loaded image to the same name as the old one.

2. Update the record to point to the replacement image.
I think this one is easier.
You'd load the image as usual, and update the existing record to reflect the new name of the image.

I think your form would need an option to indicate when a new image is being uploaded, and when it's a replacement image.
0
heymeAuthor Commented:
Hi,

Thanks for your response.
Due to the number of images that will be uploded over time, it would be best to do option one.
Please help with this one, if you can.
thank you.
0
heymeAuthor Commented:
Hi,

Please help me with this.
0
hc0904pcdCommented:
okay, I'll need to look up some fso and come back to you later on this question
0
heymeAuthor Commented:
thank you for you help, i'll wait for you.
0
heymeAuthor Commented:
sorry to bother you again,
any joy with this.
0
hc0904pcdCommented:
hi, sorry for the delay.

okay, doing it the way you say, people will need to have an option to upload a replacement image.

when they select that option, you don't need to update your database record, just upload & replace the specified image.

and, on re-reading your code, i don't think you need to specifically delete the old file using fso.
i think your process has existing features to delete / rename / copy?

this is the delete process
 FileName = Upload("File" & x).FileName
 Folder = Server.MapPath("Uploads") & "\"
 Upload.DeleteFile Folder & FileName
 Upload("File" & x).SaveAs Folder & FileName

from this example, do you see how it works?

0
heymeAuthor Commented:
hi,

I'm facing a problem other than the one you replied back to me, Please help me on this as this is more important.

I think its fare to increase the points of this question, as I dont want you to think I'm asking too many questions without giving you the write points you deserve.

the code above, works fine (not including the delete process), but when I try to upload the data without selecting an image to upload, the following error occurs:

Upload Without COM error '80040004'

Failed to save file: 2008_Feb_12-6874.958.bin. No data was found within the file (zero bytes). [Need help? Contact Yasir @ Planet Technologies - yasir@planet-tech.co.uk]

/admin/clsField.asp, line 248

I tried placing if statements to force an unavailable_image.gif if image not selected, but i'm getting problems doing this.

thanks.
0
hc0904pcdCommented:
hi
you could use javascript validation on the form page to check whether a file has been selected
and then also, in the processing page, check whether you have an input file or not.

try this,
if Upload.Form("File" & x).IsEmpty then
   'no file, so do not process
else
  'carry on ...

end if

let me know if that format works
0
heymeAuthor Commented:
Hi,

I tried that, this is what I have done:

   FileName = Upload("File" & x).FileName
   if Upload.Form("File" & x).IsEmpty then
   'no file, so do not process
else
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   sqlStr = sqlStr & "'" & FileName & "',"

end if

next
Set Upload = Nothing

I'm getting an error:
Microsoft VBScript runtime  error '800a01b6'
Object doesn't support this property or method: 'Form'
/admin/lettings_add1.asp, line 38
0
hc0904pcdCommented:
maybe the form was amistake, try again without it

if Upload("File" & x).IsEmpty then
0
hc0904pcdCommented:
or from another angle, you could try using the length property,

if Upload("File1").Length <= 1048576 then

which is meant to restrict uploads.
in the above example, only files that are less than 1 mb will continue to load.

you may not need that, but it's an inderect method of checking whether there the upload file actually exists.
0
heymeAuthor Commented:
now i'm getting this error:
Object doesn't support this property or method: 'IsEmpty'
0
hc0904pcdCommented:
okay, suggest you try the length property then
but make it more then 1 mb
0
heymeAuthor Commented:
It's not getting the names of the images when I try the lenght method:

   FileName = Upload("File" & x).FileName
if Upload("File1").Length <= 1048576 then
   'no file, so do not process
else
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   sqlStr = sqlStr & "'" & FileName & "',"

end if



I did a response.write:
INSERT INTO prop (hous_no, h_let, street, town, city, pc, p_type, beds, rec, bath, p_built, price, descr, dimen, sold, view, FileName, FileName2, FileName3, FileName4, FileName5, FileName6) VALUES ('1', '11', '1', 'Adamsdown', '111','1asdf', 'House - Detached', '1', '1', '1', 'Rent', '1', '1', '1', '0', '1', )

[Microsoft][ODBC Microsoft Access Driver] Syntax error in INSERT INTO statement.
0
hc0904pcdCommented:
I tlooks like in the above code you are checking the file size, and if it is less than 1mb you are not loading the image, but are still writing to the database
0
heymeAuthor Commented:
I changed your code slightly:
if Upload("File" & x).Length <= 10 then

the results:
INSERT INTO prop (hous_no, h_let, street, town, city, pc, p_type, beds, rec, bath, p_built, price, descr, dimen, sold, view, FileName, FileName2, FileName3, FileName4, FileName5, FileName6) VALUES ('1', 'ww', 'we', 'Adamsdown', '111','1asdf', 'House - Detached', '1', '1', '1', 'Rent', '572', '1dsasdfg dg gd ', '1sdfsg d ww', '0', '1', '1_p3[8].jpg','3_w1[14].gif','3_w1[15].gif')

[Microsoft][ODBC Microsoft Access Driver] Number of query values and destination fields are not the same.

There are 6 browser buttons to upload images, I selected 3 images to upload and left the other three empty.  but the other three images are not showing which is why i'm getting the error
0
hc0904pcdCommented:
could you show the full code here?
0
heymeAuthor Commented:

<%
Dim Upload
Dim Description
Dim FileName
Dim Folder
 
 
NumberOfUploadFiles = 6
sqlStr = ""
Set Upload = New clsUpload 
for x = 1 to NumberOfUploadFiles
 
h_no = Upload("h_no").Value 
h_let = Upload("h_let").Value 
street = Upload("street").Value 'prodn field
town= Upload("town").Value 'prods field
city = Upload("city").Value 'prods field
pc = Upload("pc").Value
p_type = Upload("p_type").Value
beds = Upload("beds").Value
rec = Upload("rec").Value
bath = Upload("bath").Value
cat = Upload("cat").Value
price = Upload("price").Value
p_des = fixforsql(Upload("p_des").Value)
p_dim = fixforsql(Upload("p_dim").Value)
sold = Upload("sold").Value
view = Upload("view").Value
 
 
FileName = Upload("FileName1").Value
if Upload("File" & x).Length is null then
   'no file, so do not process
else
   Folder = Server.MapPath("Uploads") & "\"
   FileName = Upload.UniqueName(Folder, FileName)
   Upload("File" & x).SaveAs Folder & FileName
   sqlStr = sqlStr & "'" & FileName & "',"
end if
 
next
Set Upload = Nothing
 
'now remove the trailing comma
if right(sqlStr,1) = "," then sqlStr = left(sqlStr,(len(sqlStr)-1))
 
'write 
set conn = createobject("adodb.connection")
conn.Open("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=E:\kunden\homepages\16\d222578010\odbc\121fina.mdb;")
sSQL = "INSERT INTO prop (hous_no, h_let, street, town, city, pc, p_type, beds, rec, bath, p_built, price, descr, dimen, sold, view, FileName, FileName2, FileName3, FileName4, FileName5, FileName6) VALUES ('"&h_no&"', '"&h_let&"', '"&street&"', '"&town&"', '"&city&"','"&pc&"', '"&p_type&"', '"&beds&"', '"&rec&"', '"&bath&"', '"&cat&"', '"&price&"', '"&p_des&"', '"&p_dim&"', '"&sold&"', '"&view&"', "&sqlStr&")"
 
response.Write(sSQL)
 
conn.execute(sSQL)
conn.close 'clean up
set conn=nothing
 
response.redirect("lettting_add_success.asp?str_h_no=" & h_no & "&" & "str_street=" & street)
 
%>

Open in new window

0
hc0904pcdCommented:
okay, remember that for-next loop?
the problem is that when the image is not loaded, there is value for that empty image.

add this line
   sqlStr = sqlStr & "'',"

underneath       
   'no file, so do not process

leave your test line response.write(sSQL) in place so that you can check the results
0
heymeAuthor Commented:
Now it works perfectly, thank you for your help on this.

The only part I have left is the delete images or delete record with images.

I dont understand the code you added above:
this is the delete process
 FileName = Upload("File" & x).FileName
 Folder = Server.MapPath("Uploads") & "\"
 Upload.DeleteFile Folder & FileName
 Upload("File" & x).SaveAs Folder & FileName
0
hc0904pcdCommented:
What would be the situation where people use this?
Are they adding images into an existing record?
0
heymeAuthor Commented:
Yes, I need to be able to add images to an existing record as well as edit images (if they edit an image, I would like to be able to remove the old image and replace with the new one).
0
hc0904pcdCommented:
Will you be writing a separate form page for this option of editing the record?
0
heymeAuthor Commented:
yes
0
hc0904pcdCommented:
could you show me the draft of the edit page?
0
heymeAuthor Commented:
yes, I will.  I'm making further editing to the code.  I will send it to you as soon as i finish.

thanks.
0
hc0904pcdCommented:
Hi heyme,
How are you getting on, can I help?

cheers,
hc.
0
heymeAuthor Commented:
Hi,
I had a few family issues to solve, i didnt have time to build this page  up yet. could you please help me the page for editing.

thanks
0
hc0904pcdCommented:
Sure,
Won't be able to help much today, but I'll do something tomorrow
0
heymeAuthor Commented:
I'm waiting for hc0904pcd to come back and help me with this.  Please read notes above.

thank you
0
heymeAuthor Commented:
hi,

I have increased the points (ID 20871793) with regards to the new requirement.
I made sure that hc0904pcd is credited during his help.

Please also note that hc0904pcd said that he will be helping with the final bit of this question before I can classify it as complete.

0
hc0904pcdCommented:
Hi Heyme,
You were asking for an edit page, is that right?
I'll write something up later tonight.
0
heymeAuthor Commented:
thanks hc0904pcd,

sorry to be a pain.

thanks
0
hc0904pcdCommented:
no problem
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.