Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a .aspx page where in on Button Click, I want to open a .txt file in ReadOnly Mode. Opened file will only be visible to user. User cannot edit the file and User cannot perform Save operation on it.

C#
StreamReader SR;
string S = "abc", filename = "", physicalPath ="";

string appPath = HttpContext.Current.Request.ApplicationPath;
physicalPath = HttpContext.Current.Request.MapPath(appPath);
filename = physicalPath + "\\Files\\temp.txt ";
File.SetAttributes(filename, FileAttributes.ReadOnly);
SR = File.OpenText(filename);


The above code does sets the Readonly mode to the file but the file is not visible to the user on Button click.

Kindly let me know the solution.

Thanks n Regards,
Posted
Updated 10-Jan-11 19:21pm
v2
Comments
TweakBird 11-Jan-11 1:23am    
Please use <pre> tag for code blocks.

1 solution

Well, no. The file is not visible to the user - all you have done is open a stream to read the file with. The stream is in the server memory, the file is on the server harddisk. The user is on another continent.

If you want the user to be a able to see the file in your ASPX page, then you have to explicitly show it to him. Unfortunately, that does mean he will then be in control of it: Select All, Copy will give him a copy of teh data he can save, and even if you disable selection and copy via javascript that doesn't mean he can't get at it: most browsers allow "view source" which would show the whole page content in HTML without running any JS code on it.
 
Share this answer
 
Comments
Ankur\m/ 11-Jan-11 6:09am    
One of the way I know to overcome this problem is showing the text in the file as image(s).
There are tools on the web to do that.
I have implemented a PDF Viewer using an article in the CodeProject which does the same thing.
OriginalGriff 11-Jan-11 6:14am    
Ankur: The OP should be aware though, that "Right click... Save image as" followed by OCR would result in saved info. It really isn't possible to present anything to a user that he can't save in some form or another... :laugh:

Good idea though, except for bandwidth on a large file!
Ankur\m/ 11-Jan-11 6:56am    
You are absolutely correct. But the method is way better than directly showing the contents to the user as a text if you don't want the user to copy it.
Anyways it seems OP is more concerned about not allowing to EDIT the contents of the file. So there's nothing to bother about.
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900