我想编写一个 Web 应用程序,它有两个按钮,直接从浏览器打开两个简单的文本文件(使用“文件打开”对话框或类似的东西),然后继续读取这些文件的内容并将它们存储在两个字符串中。这里的关键点是,在运行时我不知道用于读取的确切文件,因此由用户选择文件。
目标是稍后能够比较这两个字符串,但这部分我已经知道该怎么做了。我的问题是 - 是否有可能在 Web 应用程序内部执行此操作(即调用文件打开对话框以允许用户选择要读取的文件),或者安全限制或其他一些与 Web 应用程序相关的约束会阻止它从完成?
如果可能的话,我希望有一些示例代码来描述如何打开文件以及如何将所选文件的内容读入字符串。否则,我想知道这是否不可能,我应该考虑做一个桌面应用程序或尝试一种完全不同的方式。
谢谢你!
I would like to write a Web Application that would have two buttons and directly from the browser would allow me to open two simple text files (using a File Open dialog box or something similar) and would then proceed to read in contents of those files and store them inside of a two strings. The key point here is that the exact files used to read from I don't known at runtime so it would be up to the user to select the files.
The goal is to be able to later compare those two strings but that part I already know how to do. My questions is this - is it even possible to do this inside of a Web Application (i.e. to call a File Open dialog box to allow the user to select files to read from) or would security limitations or some other Web Application related constraints prevent it from being done?
If it is possible, I would appreciate some sample code describing how to open files and how to read in contents of the selected files into strings. Othwerwise, I would like to know if it's not possible and I should consider doing a desktop application or try an entirely different way.
Thank you!
发布评论
评论(4)
单独使用 JS 是不可能的。您必须构建文件上传(并存储会话信息)或使用 Silverlight 和 Javascript-Bridge 来连接您的 Web 应用程序。
以下是 Silverlight 中的 FileOpenDialog 示例:http://www. silverlightexamples.net/post/Open-File-Dialog-in-Silverlight.aspx
这是通过 C#/Webforms 上传文件的示例 http://support.microsoft.com/kb/323246
That is not possible alone with JS. You would have to build a file upload (and store session information) or use Silverlight and a Javascript-Bridge to your Web-Application.
Here's an example for a FileOpenDialog in Silverlight: http://www.silverlightexamples.net/post/Open-File-Dialog-in-Silverlight.aspx
Here's an exmaple for a file upload via C#/Webforms http://support.microsoft.com/kb/323246
这是可能的,但您最终必须将两个文本文件上传到服务器并将文件读入服务器端的字符串中。
您需要做的就是添加两个单独的 < code>FileUpload 控件到页面以及将文件发布到服务器的按钮。
如果您不希望页面刷新,您始终可以使用
AsyncFileUpload
控件。更新
读取文件内容应该相对容易(只要它们是纯文本):
It is possible, but you would wind up having to upload both of the text files to the server and read the files into strings server-side.
All you would need to do is add two separate
FileUpload
controls to the page along with a button to post the files to the server.If you don't want the page to refresh, you could always do the comparison via AJAX using the
AsyncFileUpload
control from the ASP.NET AJAX Toolkit.Update
Reading the contents of the file should be relatively easy (as long as they are plain text):
一种方法是使用 ASP.NET AJAX AsyncFileUpload 控件。
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload /AsyncFileUpload.aspx
One way is to use the ASP.NET AJAX AsyncFileUpload control.
http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx
为了从服务器端代码(C# 代码)访问文件,您需要用户上传它们。执行此操作的标准方法(出于安全原因,这是您应该依赖的唯一方法)是使用文件输入元素。在 ASP.NET 中,您可以使用 FileUpload< /a> 控制。
您实际上会为用户提供两个这样的控件,他们可以使用它们上传两个文件。然后,您可以在服务器上读取它们的内容,以您希望的方式保存它们(作为文件、保存到数据库、仅在会话中临时使用等),并对该数据执行逻辑。然后构建你的输出(比较部分,你说你已经有了)以显示在页面刷新上。
请注意一些问题,例如如果用户尝试上传非文本文件、非常大的文件等该怎么办。
In order to access the files from the server-side code (C# code) you'll need the user to upload them. The standard way to do this (and, for security reasons, the only way upon which you should rely) is with a file input element. In ASP.NET, you can use the FileUpload control.
You would essentially give the user two of these controls with which they can upload the two files. Then you'd read their contents on the server, save them however you wish (as files, to a database, just in Session for temporary use, etc.) and perform your logic on that data. Then build your output (the comparison part, which you said you have already) to display on the page refresh.
Be mindful of concerns such as what to do if the user tries to upload non-text files, very large files, etc.