文件上传用XML

听宁信息

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

XML

-

 1<guests>   
 2\- <xsd:schema id="Guests" targetnamespace="" xmlns="" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:xsd="http://www.w3.org/2001/XMLSchema">   
 3\- <xsd:element msdata:isdataset="true" name="Guests">   
 4\- <xsd:complextype>   
 5\- <xsd:choice maxoccurs="unbounded">   
 6\- <xsd:element name="Files">   
 7\- <xsd:complextype>   
 8\- <xsd:sequence>
 9<xsd:element minoccurs="0" name="title" type="xsd:string"></xsd:element>
10<xsd:element minoccurs="0" name="file" type="xsd:string"></xsd:element>
11<xsd:element minoccurs="0" name="length" type="xsd:string"></xsd:element>
12<xsd:element minoccurs="0" name="contenttype" type="xsd:string"></xsd:element>
13</xsd:sequence>
14</xsd:complextype>
15</xsd:element>
16</xsd:choice>
17</xsd:complextype>
18</xsd:element>
19</xsd:schema>   
20\- <files>
21<title>a</title>
22<file>C:\csharpexamples\banana.jpg</file>
23<length>1954</length>
24<contenttype>image/pjpeg</contenttype>
25</files>   
26\- <files>
27<title>b</title>
28<file>C:\csharpexamples\event.cs</file>
29<length>4883</length>
30<contenttype>application/octet-stream</contenttype>
31</files>   
32\- <files>
33<title>b</title>
34<file>C:\csharpexamples\event.cs</file>
35<length>4883</length>
36<contenttype>application/octet-stream</contenttype>
37</files>   
38\- <files>
39<title>ghjhhjgh</title>
40<file>D:\wwwroot\help.gif</file>
41<length>342</length>
42<contenttype>image/gif</contenttype>
43</files>   
44\- <files>
45<title>fghfghg</title>
46<file>D:\wwwroot\pagerror.gif</file>
47<length>2806</length>
48<contenttype>image/bmp</contenttype>
49</files>   
50\- <files>
51<title>sdfsfd</title>
52<file>D:\wwwroot\first.dll</file>
53<length>3584</length>
54<contenttype>application/octet-stream</contenttype>
55</files>   
56\- <files>
57<title>neelam</title>
58<file>D:\wwwroot\mmc.gif</file>
59<length>356</length>
60<contenttype>image/gif</contenttype>
61</files>
62</guests>

<-------------------!>
UP.ASPX

1@ Page Language="C#" EnableSessionState="False" 
1@ Import Namespace="System" 
1@ Import Namespace="System.IO" 
1@ Import Namespace="System.Data" 
1-- These are the imported namespaces needed to run the guest book --
 1<html>
 2<head>
 3<title>Uploading Files.</title>
 4<script language="C#" runat="server">   
 5//This method is called when the upload button is clicked   
 6public void Submit_Click(Object sender, EventArgs e)   
 7{   
 8//the path to the Xml file which will contain all the data   
 9string dataFile = "db/upload.xml" ;   
10try   
11{   
12//proceed only if the file is posted   
13if(file.PostedFile!=null)   
14{   
15errmess.Text="" ;   
16//Open a FileStream to the Database in read mode   
17FileStream fin;   
18fin= new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Read,FileShare.ReadWrite);   
19//Create a DataSet object   
20DataSet guestData = new DataSet();   
21//Read data from the Database   
22guestData.ReadXml(fin);   
23fin.Close();   
24//extract the filename from the full file path   
25string nam = file.PostedFile.FileName ;   
26int i= nam.LastIndexOf("\\\") ;   
27string newnm =nam.Substring(i) ;   
28//Create a new DataRow from the DataSet Schema   
29DataRow newRow = guestData.Tables[0].NewRow();   
30//Fill the DataRow with form values   
31newRow["title"]=title.Text;   
32newRow["file"]=file.PostedFile.FileName;   
33newRow["length"]=file.PostedFile.ContentLength.ToString();   
34newRow["contenttype"]=file.PostedFile.ContentType;   
35//Add the row to the DataSet   
36guestData.Tables[0].Rows.Add(newRow);   
37//Create another filestream to the DataBase file in write mode   
38FileStream fout ;   
39fout = new FileStream(Server.MapPath(dataFile),FileMode.Open,FileAccess.Write,FileShare.ReadWrite);   
40guestData.WriteXml(fout, XmlWriteMode.WriteSchema);   
41fout.Close();   
42//Hide the Form Panel   
43formPanel.Visible=false;   
44//Display the view Panel   
45thankPanel.Visible=true;   
46}   
47}   
48catch (Exception edd)   
49{   
50//catch any other exception that occur   
51errmess.Text="Cannot write to XML file because "+edd.ToString() ;   
52  
53}   
54}   
55</script>
56</head>
57<link href="mystyle.css" rel="stylesheet" type="text/css"/>
58<body leftmargin="0" marginheight="0" marginwidth="0" rightmargin="0" topmargin="0">   

-- Include a header file 'header.inc' --

 1<!-- #Include File="header.inc" -->
 2<asp:label id="errmess" runat="server" style="color:#FF0000" text=""></asp:label>
 3<asp:panel id="formPanel" runat="server">
 4<form action="upload.aspx" enctype="multipart/form-data" runat="server">
 5<table align="Center" border="0" width="80%">
 6<tr class="rowstyle">
 7<td>Title :</td>
 8<td><asp:textbox class="textstyle" id="title" runat="server" text=""></asp:textbox>
 9<asp:requiredfieldvalidator controltovalidate="title" display="static" runat="server">*</asp:requiredfieldvalidator></td>
10</tr>
11<tr class="rowstyle">
12<td>File :</td>
13<td><input class="textstyle" id="file" runat="server" text="" type="file"/>
14<asp:requiredfieldvalidator controltovalidate="file" display="static" runat="server">*</asp:requiredfieldvalidator></td>
15</tr>
16<tr class="rowstyle">
17<td colspan="2">
18<asp:button class="buttonstyle" id="write" onclick="Submit_Click" runat="server" text="Upload"></asp:button></td>
19</tr>
20</table>
21</form>
22</asp:panel>
23<asp:panel id="thankPanel" runat="server" visible="false">
24<p align="center" class="messagestyle"><b>Your file has been uploaded!</b><br/></p>
25<p align="center"> <a href="show.aspx">Click here </a> to view Uploaded files.</p>
26</asp:panel>
27</body>
28</html>

**show.aspx

** ``` @ Page Language="C#"

@ Import Namespace="System"

@ Import Namespace="System.IO"

@ Import Namespace="System.Data"

-- Needed Assembiles --

Uploading files
Title File Length(In Bytes) Content Type
``` # DataBinder.Eval(Container.DataItem, "title") ``` ``` # DataBinder.Eval(Container.DataItem, "file") ``` ``` # DataBinder.Eval(Container.DataItem, "length") ``` ``` # DataBinder.Eval(Container.DataItem, "contenttype") ```
```
Published At
Categories with Web编程
Tagged with
comments powered by Disqus