.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

FileUploadControl uploading and saving images and showing in DataList dynamically

Posted By:Nikhil Kumar       Posted Date: January 28, 2010    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

FileUploadControl uploading and saving images and showing in DataList dynamically. In this article we learn how to use FileUploadControl to upload Images in a folder under your web project and show them in a DataList control by using image control under ItemTemplate.
 

FileUploadControl uploading and saving images and showing in DataList dynamically:-

Here by this article we learn how to use FileUploadControl to upload Images in a folder under your web project and show them in a DataList control by using image control under ItemTemplate.

First Step:


In the Default.aspx page create a label, FileUploadControl; button and a DataList Control then Under ItemTemplate add an image control.

Default.aspx

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>





  Upload Control:DataList


  
  
Please upload your image...




<%#Eval("Name") %>


Second Step:

Now add this C# code for doing all the activities.

Default.aspx.cs


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {

  }
  protected void btnadd_Click(object sender, EventArgs e)
  {
  if (fileupload.HasFile)
  {
  if (CheckFileType(fileupload.FileName))
  {
  string filepath = "~/UploadedFiles/" + fileupload.FileName;
  fileupload.SaveAs(MapPath(filepath));
  }
  }
  }
  bool CheckFileType(string fileName)
  {
  string ext = Path.GetExtension(fileName);
  switch (ext.ToLower())
  {
  case ".gif":
  return true;
  case ".png":
  return true;
  case ".jpg":
  return true;
  case ".jpeg":
   return true;
  default:
  return false;
  }
  }
  void Page_PreRender()
  {
  string upFolder = MapPath("~/UploadedFiles/");
  DirectoryInfo dir = new DirectoryInfo(upFolder);
  dl.DataSource = dir.GetFiles();
  dl.DataBind();
  }
}




Declaration:

Here is a function named CheckFileType using for checking the file type which we are going to upload
This fuction allows only four types of image files if there is another type it will trow error, so this is for restriction.

btnadd_Click
is using to show the images in imagecontrol.

HasFile
is using to check there is there any file uploaded or not.

SaveAs
enables you to save the uploaded file to the file system.

FileName
-Enables you to get the name of the uploaded file.

Here we used

<
asp:Image ID="img" runat="server" Width="200px" ImageUrl='<%# Eval("Name", "~/UploadedFiles/{0}") %>' />

If you don't specify the Width then the output would look bad, because images show in real size but now every uploaded image show in 200px width.

Another point to learn is UploadedFiles this is a folder created in Solution Explorer in current website

When you click the btnadd button that image will save in UploadedFiles folder its good because we don't have to learn the path if we save this in system folder.



Conclution:


Here we learn how to save images in folder under project folder, how to use imagecontrol in DataList control to show images.


Reference
: This article is created by using the help of ASP.NET 3.5 Unleashed by Stephen Walther.

IF you want help regarding this article feel free to contact me I will be happy to assist you.

Thanks
Nikhil Kumar


 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