简体   繁体   中英

Is it possible to do this in a C# ASP.NET Web Application?

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 (ie 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!

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):

var reader = new StreamReader(fileUploadControl.PostedFile.InputStream);
var contents = reader.ReadToEnd();

One way is to use the ASP.NET AJAX AsyncFileUpload control.

http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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