How to find File Name using javascript - javascript

I would like to find out if a filename exists in my Image folder, how can I do this in a function using javascript?
How about call a function from Javascript to a C# File? (ON SERVER)
Thank you

JavaScript from the browser does not have access to your Image folder on your local computer.

You have to use a server side language. Javascript won't have access to your folders on the server.

you have to provide more context
if your javascript is running within browser and you want to check if file exists on client's computer - you can't
if you want to check Image folder on server - you could do AJAX request for that image and then check if HTTP response code is 200 although that would also blow up your traffic and is not a great choice
best option would be to do AJAX request that will invoke a check on server side like /check?image_name=file.jpg

JavaScript is executed by Browser as Client-side scripting so it doesn't have access to local file system to check whether file exists or not to check file exists or not you need to use Server Side Scripting.

Related

How to check a file is existence from C# or Javascript?

We have a application hosted in some other server located in another country, I am going to use that application from different country, while using that application i want to check the user's machine local 'C' drive particular path is having that file is exist or not.
How to achieve this from C# or Javascript?
From the C#
byte[] contents = null;
if (File.Exists("C:\Program Files (x86)\Fruit\fruitList.txt"))
{
using (FileStream fileStream = File.OpenRead(scannedFile))
{
contents = new byte[fileStream.Length];
}
}
but that mentioned 'C' drive path is searching in IIS server only (where we hosted the application). But i want to check in client (user's) machine.
I couldn't find a code in Javascript.
Please help me, to achieve the expected result.
Thank you.
What you are attempting to do is not easily possible. JS will not work as it is blocked from accessing the user's client machine for security reasons. C# cannot access the client at all as it runs on the server.
The alternative is for the user to manually pick the required file from their machine using a <input type="file" /> then upload that file to your server for it to be processed and validated.
Failing that you could write a program which the user installs on their machine which provides methods for your website to retrieve information from the client - however this is an incredibly involved process.
You cant access the clients drive from the server side, would be a major security issue. You will have to ask the client to tell you if they have the file or not or find another work around. This is also whys its searching in IIS server, because it thinks you are looking for a local file. Hope this helps!
In a Website (C#) you will only have access to the files of the server because is the machine who is executing the application.
For Javascript who is execute in the client, that's not possible because client file access is not allowed by design, read this article:
Javascript Security
If you need this you have to develop an application to install in the client's PC and connect the server from this application, this is the only way, you will never have access to the client files since the browser.
Use this way
C#
var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
Get the file extension from fileStream variable
Javascript:
<input id="File" type="file"/>
$("#File")[0].files[0].name.split(".")

Modifying Text file in javascript

I need to have data written to a text file in javascript. I want it to write a username and password to the text file and create a new line every time. Here is my code http://pastebin.com/24Tvdemu.
Can anyone help this has had me stumped for ages.
As Javascript in html is a client side language, you will need to send the files to the server, and save there the file. Anyway, you can prompt the user to save the file in their local machine, but it´s not usefull at least you really need that for any reason.
Check this answer Javascript: Create and save file
Some suggestions for this -
If you are trying to write a file on client machine, You can't do this in any cross-browser way. IE does have methods to enable "trusted" applications to use ActiveX objects to read/write file.
If you are trying to save it on your server then simply pass on the text data to your server and execute the file writing code using some server side language.
To store some information on the client side that is considerably small, you can go for cookies.
Using the HTML5 API for Local Storage.
More details : Is it possible to write data to file using only JavaScript?

Reading files from hard drive with javascript

My application creates .xml files and stores them on the user's hard drive; there is a default folder that I set in the web.xml to store the files, let's say D:/temp. Now after I write the files I need to read them back with javascript, I am using a javascript library that has this function mxUtils.load('URL of the file') (this function returns the content of the file), the problem is that it is giving me an error Cross origin requests are only supported for HTTP, (I now it doesn't have anything to do with the function or the library) I think the problem is that you can't read local files because of some security issues. Anyone can advise me some solution? Thanks
You cant access local filesystem using javascript.
For accessing the file using javascript , you have to upload it to a server and access it using the files url.
As stated, you cannot access a file on the filesystem strictly through Javascript. You could, however, use the input file type to upload the file to your server and then read it:
<input type="file" name="myfileinput">
Then, you can access it via the $_FILES global in PHP - other languages also provide this functionality through other means. Please note, again, that there is absolutely no way to access a file that is on someone's filesystem with Javascript without their consent (i.e. using the file input type). That would be a huge security risk - imagine going to a page and having it wipe your whole D:/ drive.
It's best to expose your files via HTTP and use mxUtils.load("http://yoursite/static/yourfile.xml").
Search for static files on Apache HTTP Server HowTo. Setup Apache to serve your xml files, make sure that you can view xml file in browser and then use the same url in mxUtils.load call.

how to read xml from FTP server in javascript

I am new to Javascript. Please tell me that how to retrieve and read XML file which is placed on ftp server in Javascript. Javascript only speaks HTTP and WebSockets (on newer browsers), and not FTP
If you are restricted to using client-side JS, it's not possible. It is possible with NodeJS, though.
If you are speaking about client side Javascript (the one that is in the browser), then this is not possible (it can understand only HTTP protocol and with the rise of HTML5 also WebSockets). FTP is completely another protocol and hence this is not possible.
Think about it this way: your JS is stored in your browser. So the whole code that will connect to your FTP and do something there is exposed to everyone. In order to connect to FTP you need to provide your credentials (your username and password). This means that everyone who wants to get them can get them. This is not nice :-).
As I understand, the thing you want to achieve - user does something on your site (click a button) and he can download the file from the ftp. In this case I would do something like this. On click I will make an ajax call or some sort of redirect ( window.open('http://yourserver/getFile.php'); ) And the script in getFile.php (php does not matter here - this is any server side script python, asp, ...) connects to your FTP server and does whatever you want.

Get filesname through javascript

How can I get list of filenames from a specified folder through javascript?
Assuming the folder you're talking about is on the client, then you can't. It'd bring up all sorts of security issues if JavaScript had that power.
If the folder is on the server then you could use some form of AJAX, with the server side code fetching the files in the specified directory and returning it back to the client.

Categories