简体   繁体   English

如何从C#或Javascript检查文件是否存在?

[英]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. 我们在另一个国家/地区的其他服务器上托管了一个应用程序,我将使用其他国家/地区的应用程序,同时使用该应用程序我要检查用户计算机本地“ C”驱动器的特定路径是否存在该文件或不。

How to achieve this from C# or Javascript? 如何从C#或Javascript实现呢?

From the C# 从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). 但是提到的“ C”驱动器路径仅在IIS服务器(我们托管应用程序的位置)中搜索。 But i want to check in client (user's) machine. 但是我想签入客户端(用户)计算机。

I couldn't find a code in Javascript. 我找不到Java代码。

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. 由于安全原因,JS无法访问用户的客户端计算机,因此JS无法使用。 C# cannot access the client at all as it runs on the server. C#在服务器上运行时根本无法访问客户端。

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. 另一种选择是让用户使用<input type="file" />从他们的计算机中手动选择所需的文件,然后将该文件上传到您的服务器以进行处理和验证。

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. 这也是为什么它在IIS服务器中搜索的原因,因为它认为您正在寻找本地文件。 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. 在网站(C#)中,您将只能访问服务器的文件,因为这是执行应用程序的计算机。

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,这是不可能的,因为设计不允许客户端文件访问,请阅读此文章:

Javascript Security JavaScript安全性

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. 如果需要此功能,则必须开发一个应用程序以安装在客户端的PC上,并通过该应用程序连接服务器,这是唯一的方法,自浏览器以来,您将永远无法访问客户端文件。

Use this way 用这种方式

C# C#

var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

Get the file extension from fileStream variable 从fileStream变量获取文件扩展名

Javascript: Javascript:

<input id="File" type="file"/>
$("#File")[0].files[0].name.split(".")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM