protected void Page_Load(object sender, EventArgs e)
{
lblError.Text = string.Empty;
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string ServerPath = Server.MapPath("images");
string FileName = FileUpload1.FileName;
string UploadPath = ServerPath + "//" + FileName;
if (FileUpload1.PostedFile.ContentType == "application/pdf" || FileUpload1.PostedFile.ContentType == "application/msword")//for file type
{
if (FileUpload1.PostedFile.ContentLength <= 4194304)//for file size in bytes 1048576 bytes=1MB
{
FileUpload1.PostedFile.SaveAs(UploadPath);
lblError.Text = " File uploaded successfully";
//Calling here a function to store in database }
else
{
lblError.Text = " plz select files less then size 4 MB";
}
}
else
{
lblError.Text = " plz select files of type .pdf and .doc";
}
}
}
images replaced your folder name on the server
after this code you call the function that store the file name(that is store in FileName variable) and
path (that is store in UploadPath Variable) in database.
if you dont solve it then let me know
fileupload
"A programmer is someone who always looks both ways before crossing a one-way street"
protected void btnPost_Click(object sender, EventArgs e)
{
string fileName;
string filePath = "Images/";
// First check if there is a file in the control
if (FileUpload.HasFile)
{
// Name the file what you want
// FileUpload.FileName is the name
// that the file is uploaded with
fileName = "[UploadedFile]" + FileUpload.FileName;
// Save the file to a folder in your
// website, here I call it 'Images'
FileUpload.SaveAs(Server.MapPath(filePath + fileName));
}
}
This is a very simple way to upload a file through the asp.net file uploader control
kkyycyclone
Member
3 Points
18 Posts
upload file
May 22, 2010 06:09 AM|LINK
i am newbie of asp.net
can some1 teach me how to upload the file(pdf , word) into the database
and able to retrieve the file and read the file at my site .
i already open a folder named pdfword to store my file
and database table named document with document ID and document Name attributes
ur help will be thxful
adeelehsan
All-Star
18319 Points
2746 Posts
Re: upload file
May 22, 2010 08:03 AM|LINK
Hi
Following step by step tutorial teaches you what you are looking for:
http://www.beansoftware.com/ASP.NET-Tutorials/Binary-Files-To-Database.aspx
asp .net 2. 0
MCPD ASP.NET 4.0 and 3.5, MCTS WSS, MOSS, SharePoint 2010, MCT
Microsoft Community Contributor Award 2011
sreejukg
All-Star
27607 Points
4122 Posts
Re: upload file
May 22, 2010 08:15 AM|LINK
refer this
http://www.4guysfromrolla.com/articles/120606-1.aspx
http://www.developer.com/net/asp/article.php/3097661/File-Uploading-Using-ASPNET.htm
My Blog
Imran khan
Member
468 Points
84 Posts
Re: upload file
May 22, 2010 08:44 AM|LINK
Hi,here is the code
.aspx file
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; } .style2 { width: 231px; } </style> </head> <body> <form id="form1" runat="server"> <table class="style1"> <tr> <td class="style2"> <asp:FileUpload ID="FileUpload1" runat="server" /> </td> <td> <asp:Button ID="btnUpload" runat="server" Text="Upload" onclick="btnUpload_Click" style="width: 60px" /> </td> </tr> <tr> <td class="style2"> <asp:Label ID="lblError" runat="server" Text="Label"></asp:Label> </td> <td> </td> </tr> </table> <div> </div> </form> </body> </html>.CS File
protected void Page_Load(object sender, EventArgs e) { lblError.Text = string.Empty; } protected void btnUpload_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string ServerPath = Server.MapPath("images"); string FileName = FileUpload1.FileName; string UploadPath = ServerPath + "//" + FileName; if (FileUpload1.PostedFile.ContentType == "application/pdf" || FileUpload1.PostedFile.ContentType == "application/msword")//for file type { if (FileUpload1.PostedFile.ContentLength <= 4194304)//for file size in bytes 1048576 bytes=1MB { FileUpload1.PostedFile.SaveAs(UploadPath); lblError.Text = " File uploaded successfully"; //Calling here a function to store in database} else { lblError.Text = " plz select files less then size 4 MB"; } } else { lblError.Text = " plz select files of type .pdf and .doc"; } } }
if you dont solve it then let me know
fileupload
ramiramilu
All-Star
95503 Points
14106 Posts
Re: upload file
May 22, 2010 02:46 PM|LINK
http://www.aspsnippets.com/Articles/Save-Files-to-SQL-Server-Database-using-FileUpload-Control.aspx
http://www.aspsnippets.com/Articles/Save-and-Retrieve-Files-from-SQL-Server-Database-using-ASP.Net.aspx
JumpStart
aditya1986
Contributor
4168 Points
760 Posts
Re: upload file
May 22, 2010 03:34 PM|LINK
Refer
http://www.codeproject.com/KB/aspnet/fileupload.aspx
http://www.aspheute.com/english/20000802.asp
http://www.aspnettutorials.com/tutorials/database/uploadingtodatabase-csharp.aspx
http://www.stardeveloper.com/articles/display.html?article=2003031201&page=1
ahsanm.m
Contributor
5254 Points
781 Posts
Re: upload file
May 23, 2010 12:48 PM|LINK
I found this project as complete solution for your problem
http://www.aspsnippets.com/Articles/Save-Files-to-SQL-Server-Database-using-FileUpload-Control.aspx
DotnetBoss | asp.net boss
ramiramilu
All-Star
95503 Points
14106 Posts
Re: upload file
May 23, 2010 03:25 PM|LINK
I already posted the same solution....
JumpStart
ahsanm.m
Contributor
5254 Points
781 Posts
Re: upload file
May 24, 2010 03:47 AM|LINK
oh.....thnx.....I may miss that
DotnetBoss | asp.net boss
Jennica
Member
500 Points
341 Posts
Re: upload file
May 24, 2010 02:33 PM|LINK
<div> <asp:FileUpload ID="FileUpload" runat="server" /> <asp:Button ID="btnPost" runat="server" Text="Upload" /> </div>and in code behind:
protected void btnPost_Click(object sender, EventArgs e) { string fileName; string filePath = "Images/"; // First check if there is a file in the control if (FileUpload.HasFile) { // Name the file what you want // FileUpload.FileName is the name // that the file is uploaded with fileName = "[UploadedFile]" + FileUpload.FileName; // Save the file to a folder in your // website, here I call it 'Images' FileUpload.SaveAs(Server.MapPath(filePath + fileName)); } }This is a very simple way to upload a file through the asp.net file uploader control