分享
 
 
 

文件上传用XML

王朝other·作者佚名  2006-01-08
窄屏简体版  字體: |||超大  

听宁信息

此文出自:http://etning.5i4k.net/aspnet/display.aspx?id=12&Fid1=2&Fid2=4

XML

- <Guests>

- <xsd:schema id="Guests" targetNamespace="" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">

- <xsd:element name="Guests" msdata:IsDataSet="true">

- <xsd:complexType>

- <xsd:choice maxOccurs="unbounded">

- <xsd:element name="Files">

- <xsd:complexType>

- <xsd:sequence>

<xsd:element name="title" type="xsd:string" minOccurs="0" />

<xsd:element name="file" type="xsd:string" minOccurs="0" />

<xsd:element name="length" type="xsd:string" minOccurs="0" />

<xsd:element name="contenttype" type="xsd:string" minOccurs="0" />

</xsd:sequence>

</xsd:complexType>

</xsd:element>

</xsd:choice>

</xsd:complexType>

</xsd:element>

</xsd:schema>

- <Files>

<title>a</title>

<file>C:\csharpexamples\banana.jpg</file>

<length>1954</length>

<contenttype>image/pjpeg</contenttype>

</Files>

- <Files>

<title>b</title>

<file>C:\csharpexamples\event.cs</file>

<length>4883</length>

<contenttype>application/octet-stream</contenttype>

</Files>

- <Files>

<title>b</title>

<file>C:\csharpexamples\event.cs</file>

<length>4883</length>

<contenttype>application/octet-stream</contenttype>

</Files>

- <Files>

<title>ghjhhjgh</title>

<file>D:\wwwroot\help.gif</file>

<length>342</length>

<contenttype>image/gif</contenttype>

</Files>

- <Files>

<title>fghfghg</title>

<file>D:\wwwroot\pagerror.gif</file>

<length>2806</length>

<contenttype>image/bmp</contenttype>

</Files>

- <Files>

<title>sdfsfd</title>

<file>D:\wwwroot\first.dll</file>

<length>3584</length>

<contenttype>application/octet-stream</contenttype>

</Files>

- <Files>

<title>neelam</title>

<file>D:\wwwroot\mmc.gif</file>

<length>356</length>

<contenttype>image/gif</contenttype>

</Files>

</Guests>

<-------------------!>

UP.ASPX

<%@ 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() ;

}

}

</script>

</head>

<LINK href="mystyle.css" type=text/css rel=stylesheet>

<body topmargin="0" leftmargin="0" rightmargin="0" marginwidth="0" marginheight="0">

<%-- Include a header file 'header.inc' --%>

<!-- #Include File="header.inc" -->

<asp:label id="errmess" text="" style="color:#FF0000" runat="server" />

<asp:Panel id=formPanel runat="server" >

<form runat="server" enctype="multipart/form-data" action="upload.aspx">

<table border="0" width="80%" align="Center">

<tr class="rowstyle" >

<td>Title :</td>

<td ><asp:textbox class="textstyle" text="" id="title" runat="server" />

<asp:RequiredFieldValidator ControlToValidate=title display=static runat=server>*</asp:RequiredFieldValidator></td>

</tr>

<tr class="rowstyle">

<td>File :</td>

<td><input type="file" class="textstyle" text="" id="file" runat="server"/>

<asp:RequiredFieldValidator ControlToValidate=file display=static runat=server>*</asp:RequiredFieldValidator></td>

</tr>

<tr class="rowstyle">

<td colspan="2" >

<asp:Button class="buttonstyle" id="write" Text="Upload" onClick="Submit_Click" runat="server"/></td>

</tr>

</table>

</form>

</asp:Panel>

<asp:Panel id=thankPanel visible=false runat=server>

<p class="messagestyle" align=center><b>Your file has been uploaded!</b><br></p>

<p align="center"> <a href="show.aspx">Click here </a> to view Uploaded files.</p>

</asp:Panel>

</body>

</html>

show.aspx

<%@ Page Language="C#" %>

<%@ Import Namespace="System" %>

<%@ Import Namespace="System.IO" %>

<%@ Import Namespace="System.Data" %>

<%-- Needed Assembiles --%>

<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>

<itemtemplate>

<tr style="background-color:beige">

<td>

<%# DataBinder.Eval(Container.DataItem, "title") %>

</td>

<td>

<%# DataBinder.Eval(Container.DataItem, "file") %>

</td>

<td>

<%# DataBinder.Eval(Container.DataItem, "length") %>

</td>

<td>

<%# DataBinder.Eval(Container.DataItem, "contenttype") %>

</td>

</tr>

</itemtemplate>

<footertemplate>

</table>

</footertemplate>

</ASP:Repeater>

</body>

</html>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有