上传到服务器某文件夹,保存路径到access数据库的代码……

解决方案 »

  1.   

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.OleDb;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;namespace jssl.inside
    {
    /// <summary>
    /// add_picture 的摘要说明。
    /// </summary>
    public class add_picture : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.Button btnOK;
    protected System.Web.UI.HtmlControls.HtmlInputFile fileSelect;
    protected System.Web.UI.WebControls.TextBox txtRe;
    protected System.Web.UI.WebControls.TextBox txtTitle;
    private Conn conn = new Conn();

    private void Page_Load(object sender, System.EventArgs e)
    {
    //
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {    
    this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion private void btnOK_Click(object sender, System.EventArgs e)
    {
    if (fileSelect.PostedFile.FileName != "")
    {
    //取得文件大小
    int filesize = fileSelect.PostedFile.ContentLength;
    if (filesize > (500 * 1024) || filesize <= 0)
    {
    //大于500K或文件不存在时返回
    Response.Write("<script language='javascript'>alert('请确认图片存在且小于500K!');</script>");
    }
    else
    {
    try
    {
    //取得文件完整路径及名称
    string fullName = fileSelect.PostedFile.FileName.ToString();
    //取得文件扩展名
    string ExName = fullName.Substring(fullName.LastIndexOf(".")); //方案一:利用GUID结构生成唯一文件名
    Guid myGuid = Guid.NewGuid();
    string NewName = myGuid.ToString();
    NewName = NewName.Replace("-","");
    NewName = NewName.ToUpper(); //方案二:利用时间生成新文件名(年+月+日+小时+分钟+秒+毫秒)
    //string NewName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString();

    //为新文件名加上原扩展名
    NewName += ExName;
    //HttpRuntime.AppDomainAppPath获取站点根目录的物理路径
    fileSelect.PostedFile.SaveAs(HttpRuntime.AppDomainAppPath + @"upload_pic\" + NewName);

    //将相关信息写入数据库
    OleDbCommand cmd = new OleDbCommand("INSERT INTO pic (src,title,re,dt) VALUES (@src,@title,@re,@dt)",conn.GetConnection());
    cmd.Parameters.Add("src",OleDbType.VarChar).Value = @"upload_pic\" + NewName;
    cmd.Parameters.Add("title",OleDbType.VarChar).Value = txtTitle.Text.Trim();
    cmd.Parameters.Add("re",OleDbType.VarChar).Value = txtRe.Text;
    cmd.Parameters.Add("dt",OleDbType.Date).Value = DateTime.Now.ToString();
    conn.open();
    cmd.ExecuteNonQuery();
    //关闭当前窗口并刷新父窗口
    Response.Write("<Script Language='Javascript''>if(window.opener != null){alert('记录添加成功!');window.opener.location.replace(window.opener.location);}window.opener=null;window.close();</Script>");


    }
    catch(Exception error)
    {
    Response.Write(error.ToString());
    }
    finally
    {
    conn.close();
    } }
    }
    }
    }
    }
      

  2.   

    File1.PostedFile.InputStream得到一个stream流
    把这个流写入byte数组,把byte数组存入数据库就行了
      

  3.   

    我要的是上传到数据库目录,access数据库中保存路径的代码
      

  4.   

    使用客户端的File Field控件上传图片 
    http://blog.csdn.net/sunnystar365/archive/2005/09/16/481963.aspx
    不过我用的是SQL数据库,不是ACCESS,楼主需要修改一下