How to check a file is existence from C# or Javascript? - 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(".")

Related

How to call java methods in a .jar file from javascript?

I know there are many similar questions on the internet but none of them could solve my doubt. So pardon me. About my project. I have a java file or program that takes a string , encrypts it and then returns the encrypted string to me.
I want to include or keep this java file (.jar file) on the webpage or the client-side. Now I know that we can upload this java program on server side and then easily communicate with it instead of calling keeping it on client-side which most people consider a bad practice.
But my purpose of keeping this jar file on the client side is that JavaScript code for encryption can be easily seen if we inspect element, so compared to it .jar file is more secure and one cannot see underlying encryption code and also if in case the there is network loss then instead of typing the entire string again and getting it encrypted again from the server, I want to save that string in such situation, get it encrypted from the jar file on the client-side so that once network problem is solved I don't need to retype whole message again and then I can directly send the message or the string to the other end or to other user on the network.
So for this I need help regarding how can I store my .jar file on webpage or client-side and then how to call methods from the .jar file using JavaScript. I know applets are deprecated, chrome does not support java. I have also seen some examples on internet like https://docs.oracle.com/javase/tutorial/deployment/applet/invokingAppletMethodsFromJavaScript.html but Its not working properly for me.
So I am in search of some ideal method by which I can first of all include or upload .jar file on client-side and then a method by which I can call java methods from .jar file using JavaScript. Please help... My earnest request...!
JavaScript code for encryption can be easily seen if we inspect element
I'm assuming you mean embedded Javascript here. Because you can refer to an external JS files which are located on the web server, not the client machine.
The browser may cache your script file somewhere locally but if one were bothered enough to dig it up, he might as well decompile your jar file which is much more easier.
Java can easily be decompiled. A jar file on the client will not be more secure than client-side JavaScript.
An encryption method that is compromised by looking at its code is not secure.
You seem to have many misconceptions... here are some facts for you to consider:
A JAR file (as either an application, WebStart or applet) running on client-side is NOT secure. If it runs on my computer, I can do anything I want. I can download the JAR file and decompile it. It's no better than JavaScript in this regard, it just takes slightly more effort and knowledge.
To protect data between client and server, you can simply use HTTPS and POST the data from web page to your server.
If client is encrypting data to you, there should be nothing to hide about the encryption process. The client generates a session key to encrypt the data, then wrap that session key by your server's public key. Then client send both encrypted data and wrapped session key to your server. Read more about Public Key Infrastructure (PKI): https://en.wikipedia.org/wiki/Public_key_infrastructure
By default a Java applet/WebStart is not granted access to disk. You must sign it to write data to disk, and user/security policies can still deny granting those access rights. (Applet - Unable to write file)
As you are aware of it, applet/WebStart is dying because of browsers rejecting plugins. There's Web Cryptography API (it's not mature, and there's no hardware support), you can try that instead. https://www.w3.org/TR/WebCryptoAPI/

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?

Certificate (pfx file) works in code but not working after Hosting from ASP Net web application

I have to verify the pfx file and get certificate details from the web base application
but its not working after hosting the page in IIS, It only work on single machine.
-First I create an exe for the fetching the details but still its not working,
(exe saves the response in one table.
And we want to fetch the response in the web Application.
It is working through Code But After Hosting this is not working.)
I am fetching the data through creating the batch files.
like :
Please give me any suggestion or Idea for how to get the data from exe to web application.
can any one have idea please tell me.
Thanks.
The certificate loading in .NET is tied to the user profile and certificate store - even though the certificate is loaded from file. When loading in a hosting environment some special flags need to be added:
// The X509KeyStorageFlags.MachineKeySet flag is required when loading a
// certificate from file on a shared hosting solution such as Azure.
private static readonly X509Certificate2 signingCertificate =
new X509Certificate2(HttpContext.Current.Server.MapPath(
"~\\App_Data\\Kentor.AuthServices.StubIdp.pfx"), "",
X509KeyStorageFlags.MachineKeySet);
Exampel from https://github.com/KentorIT/authservices/blob/master/Kentor.AuthServices.StubIdp/Models/AssertionModel.cs

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.

How to find File Name using 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.

Categories