469,319 Members | 1,950 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,319 developers. It's quick & easy.

restriction for uploading images in asp

hi...

i have an option for the users to upload their image...
but i want to allow only gif and jpg file extension....

how to check whether the uploaded image is gif or jpg,,
and display error message when invalid file uploaded..

with regards
meenu
Aug 7 '06 #1
3 1942
teju
34
hi,
you can validate using javascript before uploading the file if the it has .gif or .jpg as the extension and then according generate error message

just let me know if its works
Aug 13 '06 #2
sashi
1,754 Expert 1GB
Hi Meenu,

kindly refer to below sample code segment.. good luck and take care my fren.. :)

<!-- TWO STEPS TO INSTALL IMAGE UPLOAD PREVIEW:

1. Copy the coding into the HEAD of your HTML document
2. Add the last code into the BODY of your HTML document -->

<!-- STEP ONE: Paste this code into the HEAD of your HTML document -->

Expand|Select|Wrap|Line Numbers
  1. <HEAD>
  2. <script type="text/javascript">
  3. <!-- Begin
  4. /***** CUSTOMIZE THESE VARIABLES *****/
  5.  
  6.   // width to resize large images to
  7. var maxWidth=100;
  8.   // height to resize large images to
  9. var maxHeight=100;
  10.   // valid file types
  11. var fileTypes=["bmp","gif","png","jpg","jpeg"];
  12.   // the id of the preview image tag
  13. var outImage="previewField";
  14.   // what to display when the image is not valid
  15. var defaultPic="spacer.gif";
  16.  
  17. /***** DO NOT EDIT BELOW *****/
  18.  
  19. function preview(what){
  20.   var source=what.value;
  21.   var ext=source.substring(source.lastIndexOf(".")+1,source.length).toLowerCase();
  22.   for (var i=0; i<fileTypes.length; i++) if (fileTypes[i]==ext) break;
  23.   globalPic=new Image();
  24.   if (i<fileTypes.length) globalPic.src=source;
  25.   else {
  26.     globalPic.src=defaultPic;
  27.     alert("THAT IS NOT A VALID IMAGE\nPlease load an image with an extention of one of the following:\n\n"+fileTypes.join(", "));
  28.   }
  29.   setTimeout("applyChanges()",200);
  30. }
  31. var globalPic;
  32. function applyChanges(){
  33.   var field=document.getElementById(outImage);
  34.   var x=parseInt(globalPic.width);
  35.   var y=parseInt(globalPic.height);
  36.   if (x>maxWidth) {
  37.     y*=maxWidth/x;
  38.     x=maxWidth;
  39.   }
  40.   if (y>maxHeight) {
  41.     x*=maxHeight/y;
  42.     y=maxHeight;
  43.   }
  44.   field.style.display=(x<1 || y<1)?"none":"";
  45.   field.src=globalPic.src;
  46.   field.width=x;
  47.   field.height=y;
  48. }
  49. // End -->
  50. </script>
  51. </HEAD>
  52.  
<!-- STEP TWO: Copy this code into the BODY of your HTML document -->

Expand|Select|Wrap|Line Numbers
  1. <BODY>
  2. <div align="center" style="line-height: 1.9em;">
  3. Test it by locating a valid file on your hard drive:
  4. <br>
  5. <input type="file" id="picField" onchange="preview(this)">
  6. <br>
  7. <img alt="Graphic will preview here" id="previewField" src="spacer.gif">
  8. <br> <div style="font-size: 7pt;">
  9. Script submitted by: <a href="http://www.abrahamjoffe.com.au/">Sydney Wedding Video / DVD</a></div>
  10. </div>
  11.  
  12. <p><center>
  13. <font face="arial, helvetica" size"-2">Free JavaScripts provided<br>
  14. by <a href="http://javascriptsource.com">The JavaScript Source</a></font>
  15. </center><p>
  16. </BODY>
  17.  
Aug 15 '06 #3
Hi,

How upload and display image in asp .Pls can u suggest sir,Once we upload the image file it is inserts into database and display image it can could show where we insert....

Kindly Regards,
Aruna.K
Oct 2 '07 #4

Post your reply

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

Similar topics

7 posts views Thread by lion | last post: by
4 posts views Thread by laredotornado | last post: by
3 posts views Thread by Gavin | last post: by
pezholio
1 post views Thread by pezholio | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.