Serving Information Simply

Sunday 10 February 2013

Image and Audio File uploading in asp.net using c#

In My last post I was described about array programming in C# and .NET arrays-in-c#.net-programming
This tutorial describes image uploading in C# and .NET. Here you will get a step by step guide to complete your goal of learning about uploading the image in dotnet technology...

(A)How to upload Image:

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

Steps:

step1. Create a table in the database (abc) :->

Create table Image1
(
Id int identity (1, 1),
Image1 varchar (100)
);

step2. Coding for connectionstring in web.config

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

step 3. in My WebForm (Page1.aspx.cs):

ProviderName:

using System.Data;
using System.Data.SqlClient;

Connectionstring:

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

step 4. Uploadbttn Coding:

if (FileUpload1.HasFile)
{
    string fileName = FileUpload1.FileName.ToString();
    string uploadFolderPath = "~/Image/";
    string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
    FileUpload1.SaveAs(filePath + "\\" + fileName);
    ImageButton1.ImageUrl ="~/Image/" + "/" + FileUpload1.FileName.ToString();
}

step 5. Addbttn Coding:

conn1.Open();
SqlCommand cmd3 = new SqlCommand("insert into Image1(image1) values('" + ImageButton1.ImageUrl + "')", conn1);
cmd3.ExecuteNonQuery();
conn1.Close();

(b) How to upload  Audio File:

Create a table in the database(abc):

create table Audio1
(
id int identity(1,1),
audio1 varchar(100)
);

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

In this case add a folder(Audio) in WebForm

Uploadbttn Coding:

if(FileUpload1.HasFile)
{
    string fileName = FileUpload1.FileName.ToString();
    string uploadFolderPath = "~/Audio/";
    string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
    FileUpload1.SaveAs(filePath + "\\" + fileName);
}

                         thanks guys ......keep visiting.

No comments:

Post a Comment