Open file read-only using Response.ContentType

session101

Newcomer
Joined
May 10, 2006
Messages
23
I have a web app that opens the appropriate application based on the file extension using Response.ContentType. When I open an xls file, I would like the file to be opened in read-only permissions with Excel. How would I do this?

Here is my existing code:

switch (file.Extension)
{
case ".pdf":
Response.ContentType = "application/pdf";
break;
case ".htm":
case ".html":
Response.ContentType = "text/html";
break;
case ".txt":
Response.ContentType = "text/plain";
break;
case ".doc":
Response.ContentType = "application/msword";
break;
case ".xls":
case ".csv":
Response.ContentType = "application/xls";
break;
default:
Response.ContentType = "application/unknown";
break;
}
Response.Flush();
Response.WriteFile(file.FullName);
Response.End();


TIA.
 
You could open a Filestream with the proper access right and copy this stream to Response.OutputStream.

Never tried but don't forget to close the stream after.

I would see something like that:

-Open stream
-Copy stream to OutputStream
-Flush the response
-Close stream

Anyone can confirm that please?

session101 said:
I have a web app that opens the appropriate application based on the file extension using Response.ContentType. When I open an xls file, I would like the file to be opened in read-only permissions with Excel. How would I do this?

Here is my existing code:

switch (file.Extension)
{
case ".pdf":
Response.ContentType = "application/pdf";
break;
case ".htm":
case ".html":
Response.ContentType = "text/html";
break;
case ".txt":
Response.ContentType = "text/plain";
break;
case ".doc":
Response.ContentType = "application/msword";
break;
case ".xls":
case ".csv":
Response.ContentType = "application/xls";
break;
default:
Response.ContentType = "application/unknown";
break;
}
Response.Flush();
Response.WriteFile(file.FullName);
Response.End();


TIA.
 
Use a Response.Redirect() to the URL of your file. This will open the file, but if you want it Read-Only specify that in the security properties of your file. (Right Click, Properties, Security, and Revoke Write permissions for users and Grant for Admin) It seemed not to work at first but if you access it as a user through the Response.Redirect(filePath) and edit it, when you try to save your change Windows will prevent it.
 
Back
Top