<%@ Page Language="C#" EnableSessionState="False" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%-- These are the imported namespaces needed to run the guest book --%>
<html>
<head>
<title>Uploading Files.</title>
<script Language="C#" runat="server">
//This method is called when the upload button is clicked
public void Submit_Click(Object sender, EventArgs e)
{
//the path to the Xml file which will contain all the data
string dataFile = "db/upload.xml" ;
try
{
//proceed only if the file is posted
if(file.PostedFile!=null)
{
errmess.Text="" ;
//Open a FileStream to the Database in read mode
FileStream fin;
fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
//Create a DataSet object
DataSet guestData = new DataSet();
//Read data from the Database
guestData.ReadXml(fin);
fin.Close();
//extract the filename from the full file path
string nam = file.PostedFile.FileName ;
int i= nam.LastIndexOf("\\") ;
string newnm =nam.Substring(i) ;
//Create a new DataRow from the DataSet Schema
DataRow newRow = guestData.Tables[0].NewRow();
//Fill the DataRow with form values
newRow["title"]=title.Text;
newRow["file"]=file.PostedFile.FileName;
newRow["length"]=file.PostedFile.ContentLength.ToString();
newRow["contenttype"]=file.PostedFile.ContentType;
//Add the row to the DataSet
guestData.Tables[0].Rows.Add(newRow);
//Create another filestream to the DataBase file in write mode
FileStream fout ;
fout = new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Write,FileShare.ReadWrite);
guestData.WriteXml(fout, XmlWriteMode.WriteSchema);
fout.Close();
//Hide the Form Panel
formPanel.Visible=false;
//Display the view Panel
thankPanel.Visible=true;
}
}
catch (Exception edd)
{
//catch any other exception that occur
errmess.Text="Cannot write to XML file because "+edd.ToString() ;
<html>
<head>
<title>Uploading files</title>
<script language="C#" runat=server>
//run the script when the Page is Loaded
public void Page_Load(Object sender, EventArgs e)
{
//the path to the Xml file which will contain all the data
string datafile = "db/upload.xml" ;
try
{
//create a DataSet object
DataSet guestData = new DataSet();
//Open a FileStream to the Database
FileStream fin ;
fin = new FileStream(Server.MapPath(datafile),FileMode.Open, FileAccess.Read,FileShare.ReadWrite) ;
//Read the Database into the DataSet
guestData.ReadXml(fin);
fin.Close();
//Databind the first table in the Dataset to the Repeater
MyDataList.DataSource = guestData.Tables[0].DefaultView;
MyDataList.DataBind();
}
catch (Exception ex)
{
//catch any other exceptions that occur
errmess.Text="Cannot read from XML file because "+ex.ToString() ;
}
}
</script>
<LINK href="mystyle.css" type=text/css rel=stylesheet>
</head>
<body topmargin="0" leftmargin="0" marginwidth="0" marginheight="0" rightmargin="0">
<!-- #Include File="header.inc" -->
<asp:label id="errmess" text="" style="color:#FF0000" runat="server" />
<br>
<ASP:Repeater id="MyDataList" runat="server">
<headertemplate>
<table width="100%" style="font: 8pt verdana" align="center">
<tr style="background-color:tan">
<th>Title</th>
<th>File</th>
<th>Length(In Bytes)</th>
<th>Content Type</th>
</tr>
</headertemplate>
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Configuration;