* To display information using message box in ASP.NET page from server side code.
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('{0}');", msg);
ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}
e.g. : DisplayAJAXMessage(this, "Hello World");
OUTPUT:
* Download a file from server using link button:
protected void lnkDownloadFile_Click(object sender, EventArgs e)
{
try
{
string FileName = string.Empty;
FileName = "demo.xls"; // Any File Name with extension
string link = Server.MapPath("<~/Folder Name/>" + FileName); //Optional Folder Name
FileInfo myfile = new FileInfo(link);
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
Response.AddHeader("Content-Length", myfile.Length.ToString());
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
Response.TransmitFile(myfile.FullName);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
DisplayAJAXMessage(this, "File Not Found");
}
}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".xls":
// case ".csv":
return "application/vnd.ms-excel";
default:
return "application/octet-stream";
}
}
* Upload any file to server using C# in ASP.NET
(Note: This example shows to upload excel file (.xls) , Just change the extension of file as per requirement to upload any type file.)
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
if (UploadFile.HasFile) //FileUpload tool from ToolBox
{
string Extension = Path.GetExtension(UploadFile.PostedFile.FileName);
if (Convert.ToString(Extension).ToLower() != ".xls")
{
lblError.Text = "Please select a file with extension .xls before validation";
return;
}
}
else
{
lblError.Text = "Please select a file with extension .xls before validation";
return;
}
string uploadFileName = Path.GetFileName(UploadFile.PostedFile.FileName);
int index = uploadFileName.LastIndexOf('.');
string SheetName = uploadFileName.Substring(0, index);
string FileName = Path.GetFileName(UploadFile.PostedFile.FileName);
string FilePath = Server.MapPath("~//" + FileName); //Optional FolderName
UploadFile.SaveAs(FilePath);
}
catch (Exception ex)
{
DisplayAJAXMessage(this,"Error::"+ex);
}
}
Happy Coding

Software Developer, Academic Writing Writer
static public void DisplayAJAXMessage(Control page, string msg)
{
string myScript = String.Format("alert('{0}');", msg);
ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}
e.g. : DisplayAJAXMessage(this, "Hello World");
OUTPUT:
Fig 1: Output of DisplayAjaxMessage |
* Download a file from server using link button:
protected void lnkDownloadFile_Click(object sender, EventArgs e)
{
try
{
string FileName = string.Empty;
FileName = "demo.xls"; // Any File Name with extension
string link = Server.MapPath("<~/Folder Name/>" + FileName); //Optional Folder Name
FileInfo myfile = new FileInfo(link);
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
Response.AddHeader("Content-Length", myfile.Length.ToString());
Response.ContentType = ReturnExtension(myfile.Extension.ToLower());
Response.TransmitFile(myfile.FullName);
Response.Flush();
Response.End();
}
catch (Exception ex)
{
DisplayAJAXMessage(this, "File Not Found");
}
}
private string ReturnExtension(string fileExtension)
{
switch (fileExtension)
{
case ".xls":
// case ".csv":
return "application/vnd.ms-excel";
default:
return "application/octet-stream";
}
}
* Upload any file to server using C# in ASP.NET
(Note: This example shows to upload excel file (.xls) , Just change the extension of file as per requirement to upload any type file.)
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
if (UploadFile.HasFile) //FileUpload tool from ToolBox
{
string Extension = Path.GetExtension(UploadFile.PostedFile.FileName);
if (Convert.ToString(Extension).ToLower() != ".xls")
{
lblError.Text = "Please select a file with extension .xls before validation";
return;
}
}
else
{
lblError.Text = "Please select a file with extension .xls before validation";
return;
}
string uploadFileName = Path.GetFileName(UploadFile.PostedFile.FileName);
int index = uploadFileName.LastIndexOf('.');
string SheetName = uploadFileName.Substring(0, index);
string FileName = Path.GetFileName(UploadFile.PostedFile.FileName);
string FilePath = Server.MapPath("~/
UploadFile.SaveAs(FilePath);
}
catch (Exception ex)
{
DisplayAJAXMessage(this,"Error::"+ex);
}
}
Happy Coding

Software Developer, Academic Writing Writer
No comments:
Post a Comment