| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 636 人关注过本帖
标题:[求助]file field如何使用哦?可以举个例子吗?
只看楼主 加入收藏
pigbypig
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-5-16
收藏
 问题点数:0 回复次数:2 
[求助]file field如何使用哦?可以举个例子吗?
file field如何使用哦?可以举个例子吗?
搜索更多相关主题的帖子: field file 例子 
2007-05-24 10:50
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 

飘过~~
2007-05-24 12:43
pigbypig
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-5-16
收藏
得分:0 


1)Setting up HTML form for file uploading.
<form id="Form1" method="post" runat="server" enctype="multipart/form-data">
<input id="filMyFile" type="file" runat="server">

2)Receiving uploaded file.
protected HtmlInputFile filMyFile;
if( filMyFile.PostedFile != null )
{
// File was sent
}
else
{
// No file
}
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.PostedFile;

// Get size of uploaded file
int nFileLen = myFile.ContentLength;
// Allocate a buffer for reading of the file
byte[] myData = new byte[nFileLen];
// Read uploaded file from the Stream
myFile.InputStream.Read(myData, 0, nFileLen);

3)Saving uploaded file to disk
// Writes file to current folder
private void WriteToFile(string strPath, ref byte[] Buffer)
{
// Create a file
FileStream newFile = new FileStream(strPath, FileMode.Create);

// Write data to the file
newFile.Write(Buffer, 0, Buffer.Length);

// Close file
newFile.Close();
}
string strFilename = Path.GetFileName(myFile.FileName);
4)Saving uploaded file to database
<PRE id=pre9 style="MARGIN-TOP: 0px" nd="42">// Writes file to the database
private int WriteToDB(string strName, string strType, ref byte[] Buffer)
{
int nFileID = 0;

// Create connection
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

// Create Adapter
OleDbDataAdapter dbAdapt = new OleDbDataAdapter("SELECT * FROM tblFile", dbConn);

// We need this to get an ID back from the database
dbAdapt.MissingSchemaAction = MissingSchemaAction.AddWithKey;

// Create and initialize CommandBuilder
OleDbCommandBuilder dbCB = new OleDbCommandBuilder(dbAdapt);

// Open Connection
dbConn.Open();

// New DataSet
DataSet dbSet = new DataSet();

// Populate DataSet with data
dbAdapt.Fill(dbSet, "tblFile");

// Get reference to our table
DataTable dbTable = dbSet.Tables["tblFile"];

// Create new row
DataRow dbRow = dbTable.NewRow();

// Store data in the row
dbRow["FileName"] = strName;
dbRow["FileSize"] = Buffer.Length;
dbRow["ContentType"] = strType;
dbRow["FileData"] = Buffer;

// Add row back to table
dbTable.Rows.Add(dbRow);

// Update data source
dbAdapt.Update(dbSet, "tblFile");

// Get newFileID
if( !dbRow.IsNull("FileID") )
nFileID = (int)dbRow["FileID"];

// Close connection
dbConn.Close();

// Return FileID
return nFileID;
}
</PRE>

5)Retrieving of a file from database.
<PRE nd="59">private void Page_Load(object sender, System.EventArgs e)
{
// Check if FileID was passed to this page as a parameter
if( Request.QueryString["FileID"] != null )
{
// Get the file out of database and return it to requesting client
ShowTheFile(Convert.ToInt32(Request.QueryString["FileID"]));
}

}
</PRE>
<PRE id=pre11 style="MARGIN-TOP: 0px" nd="62">// Read file out of the database and returns it to client
private void ShowTheFile(int FileID)
{
// Define SQL select statement
string SQL = "SELECT FileSize, FileData, ContentType FROM tblFile WHERE FileID = "
+ FileID.ToString();

// Create Connection object
OleDbConnection dbConn = new OleDbConnection(GetConnectionString());

// Create Command Object
OleDbCommand dbComm = new OleDbCommand(SQL, dbConn);

// Open Connection
dbConn.Open();

// Execute command and receive DataReader
OleDbDataReader dbRead = dbComm.ExecuteReader();

// Read row
dbRead.Read();

// Clear Response buffer
Response.Clear();

// Set ContentType to the ContentType of our file
Response.ContentType = (string)dbRead["ContentType"];

// Write data out of database into Output Stream
Response.OutputStream.Write((byte[])dbRead["FileData"], 0, (int)dbRead["FileSize"]);

// Close database connection
dbConn.Close();

// End the page
Response.End();
}
</PRE>

2007-05-25 10:21
快速回复:[求助]file field如何使用哦?可以举个例子吗?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011481 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved