Explanation:
It is very easy to upload files with the help of
FileUpload Control. We can upload file in two ways. We can upload into database
in binary from. But if the file is large size and then it take more memory of
database to store the file. So we can use the best and easiest way to upload
the file into server space. To use the second method we have to create a data
table in the database. This database will store only file name and the file
link of the file so that we can access the file latter. This is more secure
than any other process. Please try to follow all steps very carefully.
The first is to open the visual studio and create an empty webpage. Now we have to create a database and a table of the database. The fields of the table should be “ID” ,“File Name” and “fileLink.” When we upload file then ‘FileName’ will store Name of the file and ‘fileLink’ will store the path of the file so that we can access the file latter. I have created a column named “ID” because this ID will help latter to access a particular file. After all give the table Name as FileUpload.
After creating a database we have to create a folder to solution explore. To create folders, go to Solution explore, right click on the project, and click to the New Folder option. Give the name of the folder ‘Files’.
Step3:
Now we have to design the web page.
i) Take a
‘label’ which show the error and Successful message.
ii) Take ‘File
Upload’ controls which select your file to upload.
iii) Take a
‘Button’ control. When you click the button then your file will upload.
Step4:
Now we have
to add some name space.
1. using System.Data;
2. using System.Data.SqlClient;
3. using System.Configuration;
4. using System.IO;
Now write
the following code on the button click event.
protected void
Button1_Click(object sender, EventArgs e)
{
string s = Path.GetFileName(FileUpload1.PostedFile.FileName);
if (s.ToString() == ""
|| s.ToString() == null)
{
Label1.Text = "Error !!. Select Your
Resume";
}
else
{
string con = ConfigurationManager.ConnectionStrings["conName"].ToString();
SqlConnection conobj = new
SqlConnection(con);
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("Files/"
+ filename.ToString()));//upload File to server
SqlCommand com = new
SqlCommand("insert
into FileUpload (ID,FileName,fileLink)values(@var1,@var2,@var3)",
conobj);
com.Parameters.Add("@var1",
SqlDbType.NVarChar);
com.Parameters["@var1"].Value
= System.DateTime.Now.Millisecond;//insert ID
com.Parameters.AddWithValue("@var2",
filename.ToString());//Insert FileName
com.Parameters.AddWithValue("@var3",
"Files/" + filename.ToString());//upload filelink
conobj.Open();
com.ExecuteNonQuery();
conobj.Close();
Label1.Text = "Update successfull";
}
}
Step5:
Open the
web.Config file and add the database connection String.
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0"/>
</system.web>
<connectionStrings>
<add name="conName" connectionString=" Data
Source=.\SQLEXPRESS;AttachDbFilename=testdb.mdf;Integrated
Security=True;Connect Timeout=30;User Instance=True"/>
</connectionStrings>
</configuration>
Run the
project and Enjoy
Output:
1. When click the button without
selecting any file
1. When click the button with selecting
a file
Download Code
No comments:
Post a Comment