请问:哪里有上传图片或者文件的源代码?我想参考以下。

acui9937 2003-07-25 11:38:35
我现在想做一个,
不过还没有头绪。
...全文
42 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
atian25 2003-07-25
  • 打赏
  • 举报
回复
标题 文件上传用XML tingningpower(原作)

关键字 asp.net C# xml



听宁信息

此文出自: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>

atian25 2003-07-25
  • 打赏
  • 举报
回复
标题 如何利用一个FILE控件,一次上传多个附件到数据库 j99616(原作)

关键字 附件,c#,上传



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;



namespace bjoffice
{
/// <summary>
/// upfiles 的摘要说明。
/// </summary>
public class upfiles : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.DropDownList files;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.WebControls.Button Button3;
protected static DataTable upfiles1;
protected static SqlDataAdapter upfiles2;

public void CreatTable()
{
SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);
SqlDataAdapter myCommand = new SqlDataAdapter("select FileName,FileType,FileLen,FileData,[ID] from Accessories where [ID]<-1", myConnection);
DataSet mydataset = new DataSet();
myCommand.Fill(mydataset, "Accessoriestable");
upfiles2 = myCommand;
upfiles1 = mydataset.Tables["Accessoriestable"];
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
if (upfiles1 == null)
{
CreatTable();
}
if( myFile.PostedFile.FileName != "" )
{
string filename1 = Path.GetFileName(myFile.PostedFile.FileName) ;
//if(upfiles1.Rows.Find(filename)==null)
DataRow[] myrow=upfiles1.Select("FileName='"+filename1.ToString()+"'");
if(myrow.Length ==0)
{
int filelen = myFile.PostedFile.ContentLength;
string filetype = myFile.PostedFile.ContentType;
Stream fstream = myFile.PostedFile.InputStream;
byte[] filedata = new byte[filelen];
fstream.Read(filedata,0,filelen);

DataRow dr = upfiles1.NewRow();
dr[0] = filename1;
dr[1] = filetype;
dr[2] = filelen;
dr[3] = filedata;
upfiles1.Rows.Add( dr );
files.DataSource = upfiles1;
files.DataBind();

}
}
else
{
Page.RegisterStartupScript("script", "<script>alert('请选择文件!');</script>");
}

}

private void Button2_Click(object sender, System.EventArgs e)
{
if(files.SelectedIndex>=0)
{
string fname=files.SelectedItem.Text;
DataRow[] myrow=upfiles1.Select("FileName='"+fname+"'");
if(myrow.Length>0)
upfiles1.Rows.Remove(myrow[0]);
files.DataSource=upfiles1;
files.DataBind();
}
else
{
Page.RegisterStartupScript("script", "<script>alert('请选择文件!');</script>");
}

}

private void Button3_Click(object sender, System.EventArgs e)
{
if( upfiles1.Rows.Count > 0 )
{
SqlCommandBuilder myscb =new SqlCommandBuilder(upfiles2);
upfiles2.InsertCommand=myscb.GetInsertCommand();
upfiles2.Update(upfiles1);
}
upfiles1 = null;
}

}
}

发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2003-07-25 11:38
社区公告

让您成为最强悍的C#开发者