473,320 Members | 2,193 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,320 software developers and data experts.

Check File Extension

butro
5
I have a form which can check whether is an image or not. But when i try to choose a real image whose name include more than one "." character, the script fails. The split function can not be detect the file extensions when the string has "." more than one. Here is my codes. This script allows only image (i mean, .jpg, .gif, etc. file extensions).


Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <Head>
  3. <Script Language=JavaScript>
  4.  
  5. function getStats(fName){
  6.  
  7. nullIMG.src = fName;
  8. fullName = fName;
  9. shortName = fullName.match(/[^\/\\]+$/);
  10. splitName = fullName.split(".");
  11. fileType = splitName[1];
  12. fileType = fileType.toLowerCase();
  13. if (fileType == 'gif' || fileType == 'jpg' || fileType == 'jpeg')
  14. {
  15. checkIt = fileSize(fullName);
  16. if (parseInt(checkIt) > 30000){alert('Max File Size is 30Kb');Form1.reset()}
  17. else {
  18. Form1.dispSize.value = checkIt;
  19. Form1.dispName.value = shortName;
  20. Form1.dispW.value = nullIMG.width;
  21. Form1.dispH.value = nullIMG.height;
  22. }
  23. else {
  24. alert("You must select an image file!");
  25. Form1.reset();
  26. }
  27. }
  28.  
  29. divStr = "<Div Style='Position:Absolute;Top:-2000'><IMG Src=Null ID=nullIMG></Div>"
  30. document.write(divStr)
  31.  
  32. </Script>
  33. <Script Language=VBScript> 
  34.  
  35. Function fileSize(fileSpec)
  36. Dim isSize
  37. Set fso = CreateObject("Scripting.FileSystemObject")
  38. Set contentFile = fso.GetFile(fileSpec)
  39. isSize = contentFile.Size
  40. fileSize = isSize
  41. Set contentFile = Nothing
  42. Set fso = Nothing
  43. End Function
  44.  
  45. </Script>
  46. </Head>
  47. <Body>
  48. <Form name='Form1'>
  49. Choose an image: <input type="file" name="isIMG" onChange="getStats(this.value)"><br>
  50. File Name: <input name='dispName' size=20><br>
  51. File Size: <input name='dispSize' size=10><br>
  52. Width: <input name='dispW' size=5><br>
  53. Height: <input name='dispH' size=5><br>
  54. <input type=Reset><br>
  55. <input type=submit value="Submit Form">
  56. </Form>
  57. </body>
  58. </html>

At that point, for example, i want to choose an image file whose name is picture.2007.05.18.gif , but with these codes i always get alert. How can i detect such these files extensions ?

Thank you
May 18 '07 #1
2 8609
iam_clint
1,208 Expert 1GB
TRY
Expand|Select|Wrap|Line Numbers
  1. fileType = splitName[splitName.length-1];
  2.  
May 18 '07 #2
butro
5
thank you for ypur aid and i solved problem
May 19 '07 #3

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

Similar topics

5
by: bart plessers | last post by:
Hello, I am developping a file browser bu use of internet explorer. I am already so far that I get a full list of all the files of a selected directory. However, I only want to display...
23
by: Mark Tranchant | last post by:
A new project: http://step-by-step.org.uk/ Don't worry about the links yet, the content hasn't been written. I'm just interested in the introduction screen, which is my first attempt at an...
2
by: David Fickbohm | last post by:
People, I am trying to determine the creation date of files in a folder. I am using the following code to find the folder and confirm that files exist in the folder. If someone could give me an...
3
by: Shapper | last post by:
Hello, I created a script to upload a file. To determine the file type I am using userPostedFile.ContentType. For example, for a png image I get "image/png". My questions are: 1. Where can...
3
by: srijan2005 | last post by:
can any one plz help me to use mime type in php....there should be included mime_content_type( ) function...i want to upload a picture like .jpg,.jpeg,.gif extension....when i will upload the picture...
0
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of...
2
by: amanzour | last post by:
How can I check the file extension of let say a file extension in the client machine? I am using ASP.NET C# language, and was hoping one of the field validators could take care of my q. but don't...
2
by: Fareast Adam | last post by:
Anybody please help me! The function always return false although file extension is supported. Here my code; function checkExtension($extension) { $valid_ext =...
10
by: maheswaran | last post by:
Hi, I want to check the extension of uploaded file. I upload the bmp image using file property. Now i am going to edit the file upload text as bmp file into gif... example : i upload...
1
by: Chris Rebert | last post by:
On Sat, Sep 27, 2008 at 3:42 PM, Michael Crute <mcrute@gmail.comwrote: Looking at the docs for the mimetypes module, it just guesses based on the filename (and extension), not the actual contents...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.