Upload an Image and an Audio file using C#

0 comments
Image Upload:

In this case, use  a FileUpload Control(Id:->FileUpload1), an ImageButton(ImageButton1) or you can use the Image,  a Button Control(Id:->Updatebttn) to upload the Image, Button Control(Id:->Addbttn) to add the Image in database, an image(which we want to upload) , add a new folder (Image) in the WebForm.

Steps:

1. Create a table in the database(abc):

1create table Image1
2(
3id int identity(1,1),
4image1 varchar(100)
5);

2. Coding for connectionstring in web.config

1<connectionStrings>
2<add name="conn" connectionString="Data Source=MAHAK\SQLEXPRESS;Initial Catalog=abc;Integrated Security=True;"/>
3</connectionStrings>

3. In My WebForm(Page1.aspx.cs):

providerName:

1using System.Data;
2
3using System.Data.SqlClient;

connectionstring:

1SqlConnection conn1 = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

4.Uploadbttn Coding:

01if (FileUpload1.HasFile)
02{
03
04    string fileName = FileUpload1.FileName.ToString();
05    string uploadFolderPath = "~/Image/";
06    string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
07
08    FileUpload1.SaveAs(filePath + "\\" + fileName);
09
10    ImageButton1.ImageUrl =
11
12    "~/Image/" "/" + FileUpload1.FileName.ToString();
13
14}

5.Addbttn Coding:

1conn1.Open();
2
3SqlCommand cmd3 = new SqlCommand("insert into Image1(image1) values('" + ImageButton1.ImageUrl + "')", conn1);
4
5cmd3.ExecuteNonQuery();
6conn1.Close();

(b) Audio File Upload:

Create a table in the database(abc):

1create table Audio1
2(
3id int identity(1,1),
4audio1 varchar(100)
5);

use FileUpload Control and a Button(Id:->Uploadbttn)

in this case add a folder(Audio) in WebForm

0 comments: