.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Upload an Image and an Audio file using C#

Posted By:Mahak Gupta       Posted Date: October 23, 2010    Points: 50    Category: ASP.NET    URL: http://www.dotnetspark.com  

Upload an Image and an Audio file using C# step by step method
 

Upload an Image and an Audio file using C#:-


(a) Image Upload:->In my case, I use  a FileUpload Control(Id:->FileUpload1), an ImageButton(ImageButton1) or U 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:->

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

create table Image1
(
id int identity(1,1),
image1 varchar(100)
);

2. Coding for connectionstring in web.config




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

providerName:->

using System.Data;

using System.Data.SqlClient;

connectionstring:->

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

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();

}

5.Addbttn Coding:->

conn1.Open();

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

cmd3.ExecuteNonQuery();
conn1.Close();

(b) Audio File Upload:->

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);

}
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend