After hearing that the sweetness that comes out of even the fruits is not good for you, i just realized what a bad job the DNA scientists are doing by making the fruits sweet. These days when you eat a fruit you feel that you are eating something sweet, it doesnt have the flavour or the aroma that we used to have in fruits that we ate earlier. My doctor says that even extremely sweet fruits are bad. Hence i ask the scientists to please leave the fruits alone. Do not make them sweet. Let them have their sweetness naturally...
Monday, 5 November 2012
Monday, 17 October 2011
Open documents like word, excel in asp.net
I was looking at ways to open word docs or excel docs in asp.net. Most of the solutions i found were related to using the Response object. Specifically using Response.Transmit.
Problem with Response.Transmit is that it does not work if your control is within UpdatePanel. Hence use the code below to open documents if your control is within updatepanel
string filePath = e.CommandArgument.ToString();
FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
string contentType = string.Empty;
if (fileInfo.Exists)
{
switch (fileInfo.Extension.ToLower())
{
case ".dwf":
contentType = "Application/x-dwf";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
contentType = "Application/vnd.ms-word";
break;
case ".docx":
contentType = "Application/vnd.ms-word";
break;
case ".ppt":
contentType = "Application/vnd.ms-powerpoint";
break;
case ".pps":
contentType = "Application/vnd.ms-powerpoint";
break;
case ".xls":
contentType = "Application/vnd.ms-excel";
break;
case ".txt":
contentType = "Application/octet-stream";
break;
case ".jpg":
contentType = "image/jpeg";
break;
default:
break;
}
}
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.Redirect(filePath, true);
Problem with Response.Transmit is that it does not work if your control is within UpdatePanel. Hence use the code below to open documents if your control is within updatepanel
string filePath = e.CommandArgument.ToString();
FileInfo fileInfo = new FileInfo(Server.MapPath(filePath));
string contentType = string.Empty;
if (fileInfo.Exists)
{
switch (fileInfo.Extension.ToLower())
{
case ".dwf":
contentType = "Application/x-dwf";
break;
case ".pdf":
contentType = "Application/pdf";
break;
case ".doc":
contentType = "Application/vnd.ms-word";
break;
case ".docx":
contentType = "Application/vnd.ms-word";
break;
case ".ppt":
contentType = "Application/vnd.ms-powerpoint";
break;
case ".pps":
contentType = "Application/vnd.ms-powerpoint";
break;
case ".xls":
contentType = "Application/vnd.ms-excel";
break;
case ".txt":
contentType = "Application/octet-stream";
break;
case ".jpg":
contentType = "image/jpeg";
break;
default:
break;
}
}
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = contentType;
Response.Redirect(filePath, true);
Friday, 10 June 2011
ASP.NET MVC handling session expiry
While working on my asp.net mvc application i came across the scenario wherein i could not handle the session expiry. I was using windows authentication and hence i could not redirect automatically to the login page on session expiry as it happens in forms authentication. If you have worked with MVC you would also find that redirecting is not as straight forward as response.redirect in normal webforms applications. Redirection does not work if there was an intercal AJAX call or partial postback.
To overcome the problem of session expiry in case of internal AJAX calls or partial postback i devised the following javascript solution.
This is a javascript function which calls a controller method. The controller method checks if the session exists and returns a flag accordingly
function CheckSession(url, urlR) {
var html = $.ajax(
{
type: "POST",
url: url,
dataType: 'html',
async: false,
data: {
},
success:
function(result) {
},
error: function(req, status, error) {
window.alert("There was an unexpected error while checking the existence of session. Kindly contact the help desk.");
}
}).responseText;
var status = true;
var resultFromController = html.split(',');
if (resultFromController[0] == 'False') {
alert('The session is expired. You will be now redirected to the Home Page.');
window.location = urlR;
status = false;
}
else if (resultFromController[0] == 'True') {
status = true;
}
return status;
};
The javascript function is called like this
var url = "<%= Url.Action("CheckSession", "Request") %>";
var urlR = "<%= Url.Action("Index", "Home") %>";
var session = CheckSession(url, urlR);
This is the Controller function that checks if the session exists
public ActionResult CheckSession()
{
ContentResult contentResult = new ContentResult();
if (Session[StringConstants.SAPSystems] == null)
{
contentResult.Content = StringConstants.False + StringConstants.Comma + "Session does not exists";
}
else
{
contentResult.Content = StringConstants.True + StringConstants.Comma + "Session exists";
}
return contentResult;
}
Subscribe to:
Posts (Atom)