http://blog.csdn.net/jxufewbt/archive/2006/04/21/671406.aspx

解决方案 »

  1.   

    http://dotnet.aspx.cc/article/58ea3515-36f2-4fd9-ac89-eaf49f59816c/read.aspx
      

  2.   

    http://community.csdn.net/Expert/topic/5341/5341805.xml?temp=.752331其中回帖和发布新贴中提交图片就是一个很好的例子,只要去掉文件格式的限制代码就可以了
      

  3.   

    刚刚完成了,给你代码
    页面代码:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>Upload</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.0">
    <meta name="CODE_LANGUAGE" content="Visual Basic 7.0">
    <meta name="vs_defaultClientScript" content="JavaScript">
    <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    <script language="javascript">
                            function addFiles(oContainer)
                            {
                                    var sLineHTML="<div><input type='file' name='files' style='width:300'><input type='button' onclick='javascript:delFileInput(this)' value='删除'></div>";
                                    oContainer.insertAdjacentHTML('beforeEnd',sLineHTML);
                            }
                            function delFileInput(oInputButton) 
                            {
                                    var divToDel=oInputButton.parentNode;
                                    divToDel.parentNode.removeChild(divToDel);
                            }
    </script>
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server" encType="multipart/form-data">
    <table align="center">
    <tr>
    <td align="center"><h1>
    <asp:label id="Title" Runat="server"></asp:label></h1>
    </td>
    </tr>
    <tr>
    <td id="TD">
    <INPUT style="WIDTH: 300px" type="file" name="Files">
    </td>
    </tr>
    <tr>
    <td>
    <asp:label id="strStatus" runat="server" BorderColor="White" BorderStyle="None" Font-Size="9pt"
    Font-Bold="True" Font-Names="宋体" Width="500px"></asp:label>
    </td>
    </tr>
    <tr>
    <td align="center">
    <BUTTON style="WIDTH: 79px; HEIGHT: 20px" onclick="javascript:addFiles(TD);" type="button">
    继续添加</BUTTON>
    <asp:Button ID="btnUpLoad" Runat="server" Text=" 上 传 " EnableViewState="False" CausesValidation="true"></asp:Button>
    <asp:Button id="Button1" Runat="server" CausesValidation="true" EnableViewState="False" Text="上传保存到数据库"></asp:Button>
    </td>
    </tr>
    <TR>
    <TD align="center">
    <asp:DataGrid id="DataGrid1" runat="server" Width="100%" AutoGenerateColumns="False" ShowHeader="False"
    GridLines="None">
    <Columns>
    <asp:HyperLinkColumn DataNavigateUrlField="url" DataTextField="file_name"></asp:HyperLinkColumn>
    <asp:ButtonColumn Text="删除" CommandName="Delete">
    <ItemStyle Wrap="False"></ItemStyle>
    </asp:ButtonColumn>
    </Columns>
    </asp:DataGrid></TD>
    </TR>
    </table>
    &nbsp;
    </form>
    </body>
    </HTML>
      

  4.   

    后台代码
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    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;
    namespace pmsx.WebPrint
    {
    /// <summary>
    /// file 的摘要说明。
    /// </summary>
    public class file : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Label strStatus;
    protected System.Web.UI.WebControls.Label Title;
    protected System.Web.UI.WebControls.Button Button1;
    protected System.Web.UI.WebControls.Button btnUpLoad;
    protected System.Web.UI.WebControls.DataGrid DataGrid1;
    public string pid;
    private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    Title.Text = "<h3>多文件上传</h3>";
    pid="DJS2007001";
    if(!IsPostBack)
    GridBind(pid);

    }
    public void GridBind(string id)
    {
    string sql="select *,'../aspx/xmfjxz.aspx?file=../upfile/'+phy_file_name as url from t_add_file where pro_id='"+ id +"'";
    DataSet ds=csqlcon.getdataset(sql);
    this.DataGrid1.DataSource=ds.Tables[0];
    DataGrid1.DataKeyField="id";
    DataGrid1.DataBind();
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnUpLoad.Click += new System.EventHandler(this.btnUpLoad_Click);
    this.Button1.Click += new System.EventHandler(this.Button1_Click);
    this.DataGrid1.ItemCreated += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemCreated);
    this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    /// <summary>
    /// 上传附件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnUpLoad_Click(object sender, System.EventArgs e)
    {

    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
    System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
    int fileCount;
    int filecount = files.Count;
    try
    {
    for(fileCount = 0;fileCount<files.Count;fileCount++)
    {
    //定义访问客户端上传文件的对象
    System.Web.HttpPostedFile postedFile = files[fileCount];
    string fileName, fileExtension;
    //取得上传得文件名,文件名加入时分秒
    // fileName = System.IO.Path.GetFileName(postedFile.FileName);
    fileName=Path.GetFileNameWithoutExtension(postedFile.FileName)+DateTime.Now.ToString("yyyyMMddssffmm")+Path.GetExtension(postedFile.FileName);
    if(fileName != String.Empty)
    {
    //取得文件的扩展名
    fileExtension = System.IO.Path.GetExtension(fileName);
    //上传的文件信息
    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
    strMsg.Append("上传文件的文件名:" + fileName + "<br>");
    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
    //保存到指定的文件夹
    postedFile.SaveAs(Server.MapPath("../upfile/") + fileName);
    }

    }
    strStatus.Text = strMsg.ToString();
    GridBind(pid);

    }
    catch(System.Exception error)
    {
    strStatus.Text = error.Message;

    }
    }
    /// <summary>
    /// 上传附件并保存数据库
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void Button1_Click(object sender, System.EventArgs e)
    {
    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
    System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
    int fileCount;
    int filecount = files.Count;
    try
    {
    for(fileCount = 0;fileCount<files.Count;fileCount++)
    {
    //定义访问客户端上传文件的对象
    System.Web.HttpPostedFile postedFile = files[fileCount];
    string fileName, fileExtension;
    //取得上传得文件名,文件名加入时分秒
    string fileNameExc = System.IO.Path.GetFileName(postedFile.FileName);
    fileName=Path.GetFileNameWithoutExtension(postedFile.FileName)+DateTime.Now.ToString("yyyyMMddssffmm")+Path.GetExtension(postedFile.FileName);
    if(fileName != String.Empty)
    {
    //取得文件的扩展名
    fileExtension = System.IO.Path.GetExtension(fileName);
    //上传的文件信息
    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
    strMsg.Append("上传文件的文件名:" + fileName + "<br>");
    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
    //保存到指定的文件夹
    postedFile.SaveAs(Server.MapPath("../upfile/") + fileName);
    }
    string sql="insert into t_add_file (PRO_ID,FILE_NAME,PHY_FILE_NAME)VALUES('"+pid+"','"+fileNameExc+"','"+fileName+"')";
    csqlcon.executesql(sql);
    }
    strStatus.Text = strMsg.ToString();
    GridBind(pid);

    }
    catch(System.Exception error)
    {
    strStatus.Text = error.Message;

    }
    } private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    string sql = "Delete From t_add_file Where id="+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); try
    {
    //删除数据
    DataSet ds=csqlcon.getdataset("select * from t_add_file where id="+this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString());
    string pfname=ds.Tables[0].Rows[0]["phy_file_name"].ToString();
    string url=Server.MapPath("../upfile/")+ pfname;
    if (System.IO.File.Exists(url) )
    {
    System.IO.File.Delete(Server.MapPath("../upfile/") + pfname);
    }
    csqlcon.executesql(sql);

    }
    catch
    {
    strStatus.Text="删除失败!";
    }
    GridBind(pid);
    } private void DataGrid1_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
    {
    if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem)
    {
    LinkButton link = (LinkButton)e.Item.Cells[1].Controls[0];
    link.Attributes.Add("onclick","javascript:return window.confirm('是否要删除此附件?');");
    }
    } }
    }
      

  5.   

    这里的数据库建表要哪些自段啊,还是只要1个pid就行了?
      

  6.   

    可以使用ftpwebrequest类来做.
    不过,如果程序运行在服务器上,下载时,也会下载到服务器上.
    上传时,如果程序运行在服务器上,下载的路径将会是服务器的路径,并非客户机的本地路径.所以,这个类比较适应于做上传程序运行在本地,文件上传到远程的环境.
      

  7.   

    哈哈,真是详细,LOOV266(神游太虚)
      

  8.   

    .net中上传文件,最主要不就是这么一句吗?htmlInputFile1.PostedFile.SaveAs(@"c:\abc\fileName.ext");