用ASP.NET实现文件上传到服务器:using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data;
using System.Data.OleDb;namespace FileUpload
{
/// <summary>
/// Summary description for WebForm1.
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
private const string MDBFILE = "FileUpload.mdb"; protected Label lblFile;
protected HtmlInputFile filMyFile;
protected System.Web.UI.WebControls.Label lblInfo;
protected System.Web.UI.WebControls.Button cmdSend;
protected System.Web.UI.WebControls.Image imgFile;
protected System.Web.UI.WebControls.Image imgDB;
protected System.Web.UI.WebControls.Label lblText1;
protected System.Web.UI.WebControls.Label lblText2;

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
} private void InitializeComponent()
{    
this.Load += new System.EventHandler(this.Page_Load);
this.cmdSend.Click += new System.EventHandler(this.cmdSend_Click);
} 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"]));
}

} // Processes click on our cmdSend button
private void cmdSend_Click(object sender, System.EventArgs e)
{
// Check to see if file was uploaded
if( filMyFile.PostedFile != null )
{
// Get a reference to PostedFile object
HttpPostedFile myFile = filMyFile.PostedFile; // Get size of uploaded file
int nFileLen = myFile.ContentLength;  // make sure the size of the file is > 0
if( nFileLen > 0 )
{
// 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); // Create a name for the file to store
string strFilename = Path.GetFileName(myFile.FileName); // Write data into a file
WriteToFile(Server.MapPath(strFilename), ref myData); // Store it in database
int nFileID = WriteToDB(strFilename, myFile.ContentType, ref myData); // Set label's text
lblInfo.Text = 
"Filename: " + strFilename + "<br>" + 
"Size: " + nFileLen.ToString() + "<p>";
// Set URL of the the object to point to the file we've just saved
imgFile.ImageUrl = strFilename;
imgFile.ToolTip = "This file was stored to as file.";
lblText1.Text = imgFile.ImageUrl; // Set URL of the the object to point to the this script with ID of the file
// that will retreive file out the database
imgDB.ImageUrl = GetMyName() + "?FileID=" + nFileID.ToString();
imgDB.ToolTip = "This file was stored in database.";
lblText2.Text = imgDB.ImageUrl;

// show the images and text
imgFile.Visible = true;
imgDB.Visible = true;
lblText1.Visible = true;
lblText2.Visible = true;
}
}
} // 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();
} // Generates database connection string
private string GetConnectionString()
{
return "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath(MDBFILE) + ";";
} // 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;
} // 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();
} // Reads the name of current web page
private string GetMyName()
{
// Get the script name
string strScript = Request.ServerVariables["SCRIPT_NAME"]; // Get position of last slash
int nPos = strScript.LastIndexOf("/"); // Get everything after slash
if( nPos > -1 )
strScript = strScript.Substring(nPos + 1); return strScript;
}
}
}

解决方案 »

  1.   

    我也刚用.NET,不过我是用ASP实现上传的,读取时就用.NET读出数据生成文件!要方法请与我联系!
    [email protected]
      

  2.   

    see these related articles:Uploading Files with ASP.NET
    http://www.aspheute.com/english/20000802.aspASP.NET File Upload
    http://www.dotnetbips.com/displayarticle.aspx?id=20Uploading and retrieving images from SQL Server
    http://www.dotnetbips.com/displayarticle.aspx?id=60Displaying Images from SQL Server database in ASP.NET DataGrid
    http://www.dotnetbips.com/displayarticle.aspx?id=101
      

  3.   

    <%@ Import Namespace="System.IO" %>
    <%@ page Language="C#" debug="true" %>
    <html>
    <head>
    <title>上传文件 , http://www.chinabs.net </title>
    <script language="C#" runat="server">
     //This method is called when the "upload" button id pressed 
    public void UploadFile(object sender , EventArgs E)
     {
       //检查上传文件不为空
       if(myFile.PostedFile!=null)
       {     
      string nam = myFile.PostedFile.FileName ;
      //取得文件名(抱括路径)里最后一个"."的索引
      int i= nam.LastIndexOf(".");
      //取得文件扩展名
      string newext =nam.Substring(i);
      //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
      DateTime now = DateTime.Now; 
      string newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString(); 
      //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
      //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
      myFile.PostedFile.SaveAs(Server.MapPath("\\test\\Distribution\\"+newname+newext)); 
      //得到这个文件的相关属性:文件名,文件类型,文件大小
      fname.Text=myFile.PostedFile.FileName;
      fenc.Text=myFile.PostedFile.ContentType ;
      fsize.Text=myFile.PostedFile.ContentLength.ToString();
       }
     }</script>
    </head>
    <body>
    <center>
    <form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data"  runat="server" >
    <table border="1" cellspacing="2" cellpadding="2" >
    <tr> <td><h5>选择要上传的文件:</h5></td</tr>
    <tr>
    <td>
    <input type="file" id="myFile" runat="server" NAME="myFile">
    </td>
    </tr>
    <tr><td>
    <input type="button"  value="上 传" OnServerClick="UploadFile" runat="server" ID="Button1" NAME="Button1">
    </td></tr>
    </table>
    </form>
    <br>
    <br>
    <table border="1" cellspacing="2">
    <tr><td><b>文件资料</b></td>
    <td>&nbsp;</td> 
    </tr>
    <tr>
    <td>文件名 :</td>
    <td><asp:label id="fname" text="" runat="server" /></td></tr>
    <tr>
    <td>文件类型 :</td>
    <td><asp:label id="fenc" runat="server" /></td></tr>
    <tr>
    <td>文件大小 :(in bytes)</td>
    <td><asp:label id="fsize" runat="server" /></td></tr>
    </table>
    <br>
    <br>
    <br>
    </center>
    </body>
    </htm