Jump to content

Check if File Exists


sbaden

Recommended Posts

I have a basic understanding of both HTML and Javascript but am having trouble figuring out how they work together. I wrote some code where I'm trying to check to see if a local file exists... Not sure this can be done. In the example code I want to use headshotPath (file path) to see if the file in the path exists. Not sure what code to use to perform the check...

<!DOCTYPE html><html lang="en">		<head>		<title>Player HS Verification 3</title>	</head>		<body>		<input type="file" name="myFile" id="file-input">		<output id="list-missing"></output>				<script>			document.getElementById('file-input').addEventListener('change', handleFileSelect, false);						function handleFileSelect(evt) {				////////////////////////								////////////////////////								var teamTri= "ARI";				var playerLastName= "Acho";				var playerFirstName = "Sam";				var fileExtention = ".tga";								var imageName =  playerLastName + "_" + playerFirstName + fileExtention;				var headshotPath = "file:///Volumes/GRAPHICS01/HEADSHOTS/HEADSHOTS_12/03_Production/NFL/NFL_Sized/" + teamTri + "/" + imageName;								document.getElementById('list-missing').innerHTML ='<ul>' + imageName + '</ul>';			}					</script>			</body></html>

Link to comment
Share on other sites

JS of browsers are sanboxed so it cant access local files. if you need to check existance of file in server you should use any server side language for that. also some newer browser supports "FILE API" which can get certain information about files.

  • Like 1
Link to comment
Share on other sites

Thank you birbal. I was afraid of that. What is the difference between not being able to access local files via javascript and HTML's <input> tag being able to select files? There's no way to do the same with javascript without the browse button right?

Link to comment
Share on other sites

that is not javascript when use input=file, it is the html which is being interpreted by browser and browser is bound to use that hardcoded behaviour(you/other website can not change that) when you use it. the reason js is sanboxed for security reason, so that unknown source dont have access to client filesystem. browser can itself access files (that is what it do with cookie files saved in your computer) but js can't (it actually can but it is blocked in browser context)

Edited by birbal
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...