code for .ASPX file
==================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="fileuploadc._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="adi" runat="server" />
<asp:Button runat="server" ID="btn" style="height: 26px" onclick="btn_Click" />
</div>
</form>
</body>
</html>
==================================================================
Download Tutorial
code for .ASPX.CS
==================================================================
protected void btn_Click(object sender, EventArgs e)
{try{
if (CheckFileFormat(adi) == true)//checking for file type is pdf or not
{
if (_ContentType(adi) == true)//checking file content
{
savedocument("aditya", "readwritedata", adi);//savedocument("filename","foldername","control_id")
//message showing in popup
ScriptManager.RegisterStartupScript(this, typeof(string), "alertUpdateStatus", "alert('file is uploaded successfully'); ", true);
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "alertUpdateStatus", "alert('error! file uploading is failed...message "+e.ToString()+"'); ", true);
}
}
public bool CheckFileFormat(System.Web.UI.WebControls.FileUpload _UPDctl)
{
try
{
string FileExtension = null;
string[] AllowedExtension = { ".pdf" };
if (_UPDctl.HasFile)
{
FileExtension = System.IO.Path.GetExtension(_UPDctl.FileName).ToLower();
for (int i = 0; i <= AllowedExtension.Length - 1; i++)
{
if (FileExtension != AllowedExtension[i])
{
return false;
}
}
//check for file size
if ((_UPDctl.PostedFile.ContentLength > 1048576))
{
return false;
}
if (!(_UPDctl.PostedFile.ContentType.ToString() == "application/pdf"))
{
return false;
}
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
public bool _ContentType(System.Web.UI.WebControls.FileUpload uplTheFile)
{
try
{
int BUFFER_SIZE = 255;
int nBytesRead = 0;
byte[] Buffer = new byte[BUFFER_SIZE + 1];
string strUploadedContent = null;
Stream theStream = uplTheFile.PostedFile.InputStream;
nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);
strUploadedContent = Encoding.ASCII.GetString(Buffer).Substring(0, 4);
if (!(strUploadedContent.Contains("PDF") | strUploadedContent.Contains("pdf")))
{
return false;
}
return true;
}
catch (Exception e)
{
return false;
}
}
public bool savedocument(string id, string path, System.Web.UI.WebControls.FileUpload _UPDctl)
{
try
{
string RealFileName = null;
string SaveAt = null;
RealFileName = id.Replace("/", "_").Trim();
if (!Directory.Exists(Server.MapPath(".") + "\\" + "readwritedata"))//checking for folder exist or not. if not then create folder
{
Directory.CreateDirectory(Server.MapPath(".") + "\\" + "readwritedata");
}
SaveAt = Server.MapPath(".") + "\\" + "readwritedata";
_UPDctl.PostedFile.SaveAs(SaveAt + "\\" + RealFileName + ".pdf");
return true;
}
catch (Exception ex)
{
return false;
}
}
//function for creating folder of given name
public string CreateFolder(string path, string fname)
{
if (!Directory.Exists(path + fname))
{
Directory.CreateDirectory(path + fname);
}
return fname;
}
=======================================================================
Download Tutorial
==================================================================
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="fileuploadc._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="adi" runat="server" />
<asp:Button runat="server" ID="btn" style="height: 26px" onclick="btn_Click" />
</div>
</form>
</body>
</html>
==================================================================
Download Tutorial
code for .ASPX.CS
==================================================================
protected void btn_Click(object sender, EventArgs e)
{try{
if (CheckFileFormat(adi) == true)//checking for file type is pdf or not
{
if (_ContentType(adi) == true)//checking file content
{
savedocument("aditya", "readwritedata", adi);//savedocument("filename","foldername","control_id")
//message showing in popup
ScriptManager.RegisterStartupScript(this, typeof(string), "alertUpdateStatus", "alert('file is uploaded successfully'); ", true);
}
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(this, typeof(string), "alertUpdateStatus", "alert('error! file uploading is failed...message "+e.ToString()+"'); ", true);
}
}
public bool CheckFileFormat(System.Web.UI.WebControls.FileUpload _UPDctl)
{
try
{
string FileExtension = null;
string[] AllowedExtension = { ".pdf" };
if (_UPDctl.HasFile)
{
FileExtension = System.IO.Path.GetExtension(_UPDctl.FileName).ToLower();
for (int i = 0; i <= AllowedExtension.Length - 1; i++)
{
if (FileExtension != AllowedExtension[i])
{
return false;
}
}
//check for file size
if ((_UPDctl.PostedFile.ContentLength > 1048576))
{
return false;
}
if (!(_UPDctl.PostedFile.ContentType.ToString() == "application/pdf"))
{
return false;
}
return true;
}
else
{
return false;
}
}
catch (Exception ex)
{
return false;
}
}
public bool _ContentType(System.Web.UI.WebControls.FileUpload uplTheFile)
{
try
{
int BUFFER_SIZE = 255;
int nBytesRead = 0;
byte[] Buffer = new byte[BUFFER_SIZE + 1];
string strUploadedContent = null;
Stream theStream = uplTheFile.PostedFile.InputStream;
nBytesRead = theStream.Read(Buffer, 0, BUFFER_SIZE);
strUploadedContent = Encoding.ASCII.GetString(Buffer).Substring(0, 4);
if (!(strUploadedContent.Contains("PDF") | strUploadedContent.Contains("pdf")))
{
return false;
}
return true;
}
catch (Exception e)
{
return false;
}
}
public bool savedocument(string id, string path, System.Web.UI.WebControls.FileUpload _UPDctl)
{
try
{
string RealFileName = null;
string SaveAt = null;
RealFileName = id.Replace("/", "_").Trim();
if (!Directory.Exists(Server.MapPath(".") + "\\" + "readwritedata"))//checking for folder exist or not. if not then create folder
{
Directory.CreateDirectory(Server.MapPath(".") + "\\" + "readwritedata");
}
SaveAt = Server.MapPath(".") + "\\" + "readwritedata";
_UPDctl.PostedFile.SaveAs(SaveAt + "\\" + RealFileName + ".pdf");
return true;
}
catch (Exception ex)
{
return false;
}
}
//function for creating folder of given name
public string CreateFolder(string path, string fname)
{
if (!Directory.Exists(path + fname))
{
Directory.CreateDirectory(path + fname);
}
return fname;
}
=======================================================================
Download Tutorial
No comments:
Post a Comment