Avatar of error77
error77
 asked on

File pre-upload check with javascript

Hi all,

I need some javascript code that I can use to do the following:

1. Check and restrict what filetype I'm trying to upload
2. Check if I'm trying to submit nothing
3. and check the size and dymentions of image files if possible

I need the javascript to work before or when I click the submit button to upload

Hope someone can help

Thanks
JavaScript

Avatar of undefined
Last Comment
Michel Plungjan

8/22/2022 - Mon
Michel Plungjan

error77

ASKER
I'm not looking for an upload script. I have one already.

What I'm looking for is some javascript that I can call before it uploads that checks what I'm trying to upload via form input type file.

Hope this helps

thanks
Badotz

Checks what, the file name specified in the element?

You cannot do much with "<input type="file"... />", for good reason. I don't think it can be manipulated in the way you want, but I may be missing something?
Your help has saved me hundreds of hours of internet surfing.
fblack61
Samuel Liew

Badotz, I think you may be able to get the filename with JavaScript like a normal textbox, but not set it.

TS, to answer your questions:

1) You can only check the extension of the file name, but not the MIME type.
2) This can be done
3) This is not possible until the file has been uploaded to the server. (It only can be done with server-side coding language like PHP)
Samuel Liew

Example
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>File Field Value</title>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script type="text/javascript">
$(document).ready(function() {
	$('#button').click(function() {
		alert($('#file').val());
	});
	$('#button2').click(function() {
		$('#file').val("test.html");
	});
});
</script>
</head>

<body>
<form action="" method="get">
	<p>
		<input id="file" type="file" />
	</p>
	<p>
		<input id="button" type="button" value="Show Value of File Field" />
	(This can be done)</p>
	<p>
		<input id="button2" type="button" value="Set Value of File Field" />
	(Proof that this cannot be done)</p>
</form>
</body>
</html>

Open in new window

Michel Plungjan

You are looking for something that can check the size and extension. I posted a script that can do just that. It has settings to do what you want so why write a new one???

Features


    * multiple file select, progress-bar in FF, Chrome, Safari
    * drag-and-drop file select in FF, Chrome
    * uploads are cancellable
    * no external dependencies
    * doesn't use Flash
    * fully working with https
    * keyboard support in FF, Chrome, Safari
    * tested in IE7,8; Firefox 3,3.6,4; Safari4,5; Chrome; Opera10.60;

// validation    
// ex. ['jpg', 'jpeg', 'png', 'gif'] or []
allowedExtensions: [],        
// each file size limit in bytes
// this option isn't supported in all browsers
sizeLimit: 0, // max size  
minSizeLimit: 0, // min size


⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
error77

ASKER
Here is my current code, just in case it can be modified of in case it helps in any way.

See attached

Thanks


<script type="text/javascript">
				function displaymessage()
				{
				alert("Thanks for uploading!");
				}
				</script>
				
				
				<script language="JavaScript" type="text/javascript">
					/*<![CDATA[*/
					function Ck(obj){
					 val=obj.value.substring(obj.value.length-4,obj.value.length);
					 
					 if (val!='.jpg'&&val!='.gif'&&val!='.png'&&val!='.tif'&&val!='.jpeg'&&val!='.mov'&&val!='.mp4'&&val!='.flv'&&val!='.swf'){
					  alert(val+' is not allowed. Please choose another file type.');
					  return false;
					 }
					}
				</script>
				
				<script type="text/javascript">
					function enableSubmitBtn(toShow){
						if(toShow){
							document.getElementById('mySubmit').style.display = "block";
						}else{
							document.getElementById('mySubmit').style.display = "none";
						}
					}
				</script>
				<style type="text/css">
				.hide{
					display:none;
				}
				</style>
				
				<style type="text/css">
				.hide{
					display:none;
				}
				
				input[type=checkbox], input[type=radio] {
    			vertical-align: middle;
   			    position: relative;
   			    bottom: 1px;
  					}
  					input[type=radio] {
    				bottom: 2px;
 					 }

				
				</style>
				
				
				
				
				
				
				
				
				
						<?php
		
													
												
												
													
													echo $form->create('Upload',array('type'=>'file', 'url' => 'uploads','class'=>'form','onSubmit'=>'displaymessage()')); 
													echo $form->input('0.file',array('type'=>'file','label'=>'File Upload:','onchange'=>'Ck(this)'));
													
													echo $form->input('comp_id', array('type'=>'hidden', 'value'=>$c_id));
													
													echo '<div style="padding:10px;">&nbsp;</div>';
													echo '<label><input type="checkbox" id="enableSubmit" onclick="enableSubmitBtn(this.checked);">';
													echo '&nbsp;&nbsp;-&nbsp;&nbsp;<a href="'.$this->webroot.'tac" rel="shadowbox;width=890" style="font-size:100;">I agree to the terms and conditions.</a>';
													echo '</label>';
													
													
													echo '<span class="hide" name="mySubmit" id="mySubmit">';
												
													echo $form->end('Submit');
													echo '</span>';
													
												
		
												?>

Open in new window

ASKER CERTIFIED SOLUTION
Michel Plungjan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
error77

ASKER
Thanks
Badotz

mplungjan: JSLint does not like your Line 8:

if (!Ck(val.substring(val.lastIndexOf('.'));) {

"Problem at line 8 character 46: Expected ')' and instead saw ';'"

This seems to work:

if (!Ck(val.substring(val.lastIndexOf('.')))) {
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Michel Plungjan

Thanks