Server.mappath(“.”) method:
Server.mappath method can be used to get physical path of website. In real time scenario physical path of the website can be accessed with in webpage using server.mappath for better code maintenance
File uploading:
->moving the file from client system to website system is called “file uploading”.
->asp.net is providing fileupload control to work with file uploading
Properties:
1)hasfile:it returns true if file is uploaded else returns false
2)filename:it returns user uploaded file name
3)postedfile.contenttype:it returns type of the file uploaded by user
EX:image/gif->gif file
Text/plain->textfile
4)postedfile.saveas(file path)
This method will save user uploaded file with I server system with specified path
5)postedfile.contentlength:
It returns user uploaded file memory size in bytes
Creating image gallery website using file upload control
Goto visual studio
Start->run->devenv
It will display main window of visual studio
File menu->new->website->visual c#->select asp.net empty website
Weblocation->e:\aspnet\gallerysite[drive:\dir\websitename]
Visual studio create a folder with website name, in this folder website related files will be placed
Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
Goto design part design like below
Show gallery[hyperlink]
Upload image[label]
File upload control
upload |
(button)
Label2
Properties of hyperlink
Text-show gallery
Navigateurl-gallery.aspx
Upload button click logic(double click on upload button)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// upload button click logic
if(FileUpload1.HasFile)
{
if(FileUpload1.PostedFile.ContentType==("image/gif"))
{
// uploaded file is gif, save with in physical path of website with the same name
string path=Server.MapPath(".")+"\\"+FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(path);
Label2.Text="uploaded success fully..";
}
else
Label2.Text="upload gif file...";
}
else
Label2.Text="select file..";
}
}
Add webform
Goto view menu and select solution explorer
Right click on website path and select add new item
Select webform
Give name as gallery.aspx
Place bulletedlist control
Properties of bulletedlist1 control
Borderstyle-groove
Borderwidth-8
Displaymode-hyperlike
Font size-x-large
Page load logic
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class gallery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.IO.DirectoryInfo obj = new System.IO.DirectoryInfo(Server.MapPath("."));
BulletedList1.DataSource = obj.GetFiles(".gif");
BulletedList1.DataBind();
}
}
Goto solution explorer
Rightclick on default.aspx and select set as start page
Goto contrl F5