View Full Version : ASP file upload - check dimensions?
student101
02-29-2008, 07:48 AM
There is a security issue in Firefox and IE7 that stops the upload script from seeing the image dimensions, ie: width, height and so on. (This is on the client side only)
All I need is a work around or a script that can do this.
The idea I though of: :D
upload.asp =>
A form used to upload the file
checkimg.asp =>
Check the file dimensions - if happy upload else cancel or delete
I have these: (can't get them to work together) :(
upload script (can't get the uploaded file name for some reason)
script that can check the dimensions.
script to delete the image.
Any help would be great here?
Thanks.
Whatever Jr.
02-29-2008, 09:41 AM
Hi,
What is it you want?
Create a x-browser client-side script that checks the dimensions of an image an prevents uploading if the size is incorrect?
or
Upload an image and create a server-side script to determine its dimensions and do something if the dimensions are incorrect?
or
Both?
Tom
student101
02-29-2008, 09:48 AM
Well they are both the same really.
The problem is that client-side may not work due to client-side browser issues.
So the server side would probably be better if not safer?
Cheers
student101
02-29-2008, 09:54 AM
This code:
<%
dim iWidth, iHeight, iType
sub ImgDimension(img)
dim myImg, fs
Set fs= CreateObject("Scripting.FileSystemObject")
if not fs.fileExists(img) then exit sub
set myImg = loadpicture(img)
iWidth = round(myImg.width / 26.4583)
iHeight = round(myImg.height / 26.4583)
iType = myImg.Type
select case iType
case 0
iType = "None"
case 1
iType = "Bitmap"
case 2
iType = "Metafile"
case 3
iType = "Icon"
case 4
iType = "Win32-enhanced metafile"
end select
set myImg = nothing
end sub
' so if you whant to test it in asp just give the path to your image
ImgDimension(Server.MapPath("images/") & "images\1click.JPG")
response.write("Dimensions: " & iWidth & " x " & iHeight & "<br>")
response.write("Image Type: " & iType & "<br>")
%>
only outputs this:
Dimensions: x
Image Type:
student101
02-29-2008, 09:56 AM
Ok now it works if I change this:
ImgDimension(Server.MapPath("./") & "\images\1click.JPG")
Whatever Jr.
02-29-2008, 11:58 AM
No,
A client-side script and a server-side script are 2 completely different things.
A client-side script may not work because of security settings or other incompability issues.
Your ASP script resides on the server and doesn't case what browser you use.
If you can't combine both scripts, check with you host, maybe they have components installed to handle uploads.
HTH, Tom
student101
02-29-2008, 12:29 PM
Cool, I get it.
I would prefer not to have the host load components, some will charge heavy and others wont do it.
Server or client side as long as it works.
I guess the only thing left to do is make this script upload the file:
<form action="uploadAction" method="post" enctype="multipart/form-data" name="form1">
<input name="file" type="file" id="file">
<input type="submit" value="Upload"/>
</form>
Will have to send form data to this code below:
<%
dim iWidth, iHeight, iType
sub ImgDimension(img)
dim myImg, fs
Set fs= CreateObject("Scripting.FileSystemObject")
if not fs.fileExists(img) then exit sub
set myImg = loadpicture(img)
iWidth = round(myImg.width / 26.4583)
iHeight = round(myImg.height / 26.4583)
iType = myImg.Type
select case iType
case 0
iType = "None"
case 1
iType = "Bitmap"
case 2
iType = "Metafile"
case 3
iType = "Icon"
case 4
iType = "Win32-enhanced metafile"
end select
set myImg = nothing
end sub
' so if you whant to test it in asp just give the path to your image
ImgDimension(Server.MapPath("images/") & "images\1click.JPG")
' will have to change above code to catch the filename
response.write("Dimensions: " & iWidth & " x " & iHeight & "<br>")
response.write("Image Type: " & iType & "<br>")
%>
Whatever Jr.
02-29-2008, 01:37 PM
And where is the part that actually transfers the image from your browser to the server?
You'll need to write it before you can check its dimensions.
Tom
student101
02-29-2008, 01:40 PM
Got a working copy of it.
Could I PM it you?
Just my if-then-else-end if is not so good!!!sh1t
Whatever Jr.
02-29-2008, 01:59 PM
Sure, PM me.
student101
02-29-2008, 02:08 PM
There are problems though!!!sh1t
It only allows certain files to be uploaded and doesn't check the dimensions properly.
Will send it you.
My if-then-else-end if sux
student101
02-29-2008, 03:12 PM
This part isn't working.
if iWidth <= "300" then
response.write("Dimensions: " & iWidth & " x " & iHeight & "<br>")
response.write("Image Type: " & iType & "<br>")
else
' This is where we delete the file!
response.write("Problem:"&"<br>"&"Image dimensions are not correct, sorry"&"<br>")
response.write("Image deleted"&"<br>")
end if
That part I can't get right, it may even be the image checking part that's the problem as it only allows certain files to be uploaded - otherwise it throws errors.
Whatever Jr.
02-29-2008, 03:18 PM
iWidth should contain an integer, not a string.
The script only says the image is deleted, I don't see any deleting.
Does that help? Only showing us bits and pieces of your code makes it hard to troubleshoot. Can't you post the complete code?
Tom
student101
02-29-2008, 03:26 PM
Change it and it helps, but it only allows certain files to be uploaded. It wont allow ".PNG" files
Error Type:
Microsoft VBScript runtime (0x800A01E1)
Invalid picture: 'loadpicture'
<%
dim iWidth, iHeight, iType
sub ImgDimension(img)
dim myImg, fs
Set fs= CreateObject("Scripting.FileSystemObject")
if not fs.fileExists(img) then exit sub
set myImg = loadpicture(img)
iWidth = round(myImg.width / 26.4583)
iHeight = round(myImg.height / 26.4583)
iType = myImg.Type
select case iType
case 0
iType = "None"
case 1
iType = "Bitmap"
case 2
iType = "Metafile"
case 3
iType = "Icon"
case 4
iType = "Win32-enhanced metafile"
end select
set myImg = nothing
end sub
' so if you whant to test it in asp just give the path to your image
ImgDimension(Server.MapPath("./") & "\images\" & myvar)
if iWidth <= 300 then
response.write("Dimensions: " & iWidth & " x " & iHeight & "<br>")
response.write("Image Type: " & iType & "<br>")
else
' This is where we delete the file before we delete the record!
Set File = CreateObject("Scripting.FileSystemObject")
ImagePath = Server.MapPath("images\")
ImagePath = ImagePath & "\" & (myvar)
' check if file exists and if true delete the file
response.write("Problem:"&"<br>"&"Image dimensions are not correct, sorry"&"<br>")
response.write("Image deleted"&"<br>")
end if
%>
Whatever Jr.
02-29-2008, 03:41 PM
I'm sorry, but this isn't going to work.
The code I posted (sub ImgDimension) is an example on how to calculate the dimensions of an image.
You'll need to rewrite it for your purpose.
- create a routine that uploads the file.
- create a routine that returns the dimensions of the file.
- checks values from previous function against min/max size.
- if size is not correct delete image
Tom
student101
02-29-2008, 03:44 PM
It does work.
It just doesn't allow .png files - otherwise it throws an error, yet still uploads the image.
All I need to do is add a pattern like this:
Set imgtype= New RegExp
With imgtype
.Pattern = "gif|jpg"
.IgnoreCase = True
End With
No clue how or where to add it in though?
student101
02-29-2008, 04:04 PM
It works 100% now.
Would love to send you copy of this master piece.
Add your mail addres like this:
emailname[at]domain[dot]com
Then you can delete it afterwards.
Cheers
student101
02-29-2008, 04:18 PM
One more thing:
Is this server-side or client-side code that we have created?
Cheers and THANK YOU!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.