Click here to Skip to main content
15,878,945 members
Articles / Database Development

Upload an Image and an Audio file using C#

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
2 Mar 2014CPOL 46.5K   9   1
In my case, I use  a FileUpload Control(Id:->FileUpload1), an ImageButton(ImageButton1) or you can use the Image,  a Button Control(Id:->Updatebttn)

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

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:
1. Create a table in the database(abc):->
SQL
create table Image1
(
id int identity(1,1),
image1 varchar(100)
);
2. Coding for connectionstring in web.config
XML
<connectionStrings>
<add name="conn" connectionString="Data Source=MAHAK\SQLEXPRESS;Initial Catalog=abc;Integrated Security=True;"/>
</connectionStrings>
3. In My WebForm(Page1.aspx.cs):
providerName:
C#
using System.Data;
using System.Data.SqlClient;
connectionstring:
C#
SqlConnection conn1 = newSqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
4. Uploadbttn Coding:
C#
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:
C#
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):
SQL
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:
C#
if(FileUpload1.HasFile)
{
string fileName = FileUpload1.FileName.ToString();
string uploadFolderPath = "~/Audio/";
string filePath = HttpContext.Current.Server.MapPath(uploadFolderPath);
FileUpload1.SaveAs(filePath + "\\" + fileName);
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

755 members

Comments and Discussions

 
SuggestionVery nice. Pin
Member 131721414-Jul-17 3:10
Member 131721414-Jul-17 3:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.