inserting and fetching image using fileupload control.....

Ankitsrist
Posted by Ankitsrist under ASP.NET AJAX category on | Points: 40 | Views : 1032
hey techies...
in this code i will tell u how to use file upload control, firstly it is gud to store image in ur application folder instead of database if u are using any web host site...bcoz it consumes more space....so following is the code check it out....first take one fileupload control, button nd image control....


using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class uploadpic : System.Web.UI.Page
{
dbClass db = new dbClass();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button2_Click(object sender, EventArgs e)
{
StartUpLoad();

}
private void StartUpLoad()
{
//get the file name of the posted image
string imgName = FileUpload1.FileName;
//sets the image path
string imgPath = "images/" + imgName;
//get the size in bytes that
int imgSize = FileUpload1.PostedFile.ContentLength;
//validates the posted file before saving
if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "") {
// 10240 KB means 10MB, You can change the value based on your requirement
if (FileUpload1.PostedFile.ContentLength > 10240)
{
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('File is too big.')", true);
}
else {
//then save it to the Folder
FileUpload1.SaveAs(Server.MapPath(imgPath));
Image1.ImageUrl = "~/" + imgPath;
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Alert", "alert('Image saved!')", true);
}
}

}

Comments or Responses

Login to post response