Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
1.67/5 (2 votes)
See more:
C#
Response.ContentType = "Application/pdf";
    Response.AppendHeader("Content-Disposition", "attachment; filename=HT_2_hallticket.pdf");
    Response.TransmitFile(Server.MapPath("~/QuestionBank/Malabar College of Engineering And Technology/HT_2_hallticket.pdf"));
    Response.End();
Posted
Comments
Member 11026295 21-Aug-14 3:23am    
I don't want to copy my pdf file also.

Here is one fundamental thing: a Web application does not have control over the client systems. From the standpoint of such applications, the client system is not even obliged to have a file system. The Web application simply provides file content of certain content file. "Saving a file" is a functionality of a browser. And a browser does what it does; you don't even know which browser is used on the client side.

So, not only you have no control over the file attributes, but, strictly speaking, for the server side this client-side file does not even exist as a notion; that is the question makes no sense.

Now, let's look at the practical aspect: what can give you having a file read-only? Nothing, really. First, a lot of software can simply ignore this flag. And of course you cannot do anything with file permissions, which would need privilege elevation. The file on the server side (if there is a file) will remain intact in all cases. This is all what matters. If a user looses the file, it can be downloaded again.

And, finally, if your goal is to keep the file non-modifiable, keeping it read-only is just not serious. For such things, digital signature exists.

—SA
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-14 9:24am    
Thank you, Tadit.
—SA
Welcome. :)
You need to understand the problem first.

So, analyze the requirement first. When you say that, file will be read only, then what is the purpose of the file. Why user would download that? Is there something exists which is sensible and if user edits and submits, then it would create problems or not. So, think before implementing.
 
Share this answer
 
v3
Comments
Karthik Harve 21-Aug-14 3:12am    
Good catch :)
Sergey Alexandrovich Kryukov 21-Aug-14 3:24am    
Not true. Please see my comment below and my answer.
—SA
Okay Sergey. Got you. Modifying the answer now. :)

Thanks...
Sergey Alexandrovich Kryukov 21-Aug-14 3:24am    
This is not true. You can take some information from the file, create a new PDF file and save it under the name of the original file.

The problem is more fundamental: from the standpoint of the server side, the client-side file, strictly, speaking, simply does not exists as a notion. Server side does not "know" what happens with the downloaded content.

Please see my answer.

—SA
Bark Ups 21-Aug-14 3:49am    
Sergey is right. It is quiet simple to understand.
fileattribute - mean file can be opened (read) , deleted, append, executed. - gives you permission what you can do with a file.

if file can be accessed and read and user can read content (show on screen) it is also possible to copy it. Even if 'copy' option is not alloved you can still sit down and write it down or make screenshot (wich is also a copy of content). showing content on the screen is equal to copy.

Answering your question: How to make file readonly (c#)

check this out: http://msdn.microsoft.com/en-us/library/system.io.file.setattributes(v=vs.110).aspx


P.S. What exactly you are trying to achive?
Member 11026295 wrote:
I don't want to copy my pdf file also.
Sorry, it does now depend on what you want. It simply suggests you have no idea what the Web does in general, otherwise you would realize this very basic thing: if some content can be looked through on a client side, it is already downloaded. Too late to want something. :-)

Moreover, in real practice, the PDF content is not only downloaded by this moment, but also saved in some file on the client computer. Even if the user did not use "Save As", even if the PDF is rendered in the browser. By the way, rendering in the browsers is not supported by all systems and browsers and should not be assumed. Any non-nonsense user can have this file, just face it.

—SA
 
Share this answer
 

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