ASP.NET 用C#语言,怎么上传文件?把文件的路径存在数据库里,把文件上传到服务器的某个目录,能不能给我一个具体的例子,和代码,谢谢了

解决方案 »

  1.   

    http://www.cnblogs.com/sekihin/archive/2007/07/13/817423.html
      

  2.   

    百度或者Google    FileUpload 控件 代码很多
      

  3.   

    fileupload控件,搜下文件上传就有好多的
      

  4.   

    http://www.open-open.com/ajax/Upload.htm
      

  5.   

    protected void btnUpLoad_Click(object sender, EventArgs e)
        {
            //获取准备上传的文件的名称
            string fileName = fuConver.FileName;
            //获取准备上传文件的物理路径
            string filePath = fuConver.PostedFile.FileName;
            //获取准备上传的文件的大小
            int length = fuConver.PostedFile.ContentLength;
            //当文件小于2000000字节,即2M时:
            if (length < 2000000)
            {
                //获取文件的后缀名
                string exec = Path.GetExtension(fileName);
                if (exec.ToUpper() == ".GIF" || exec.ToUpper() == ".JPG")
                {
                    string newName = DateTime.Now.ToString("yyyyMMddhhmmss") + DateTime.Now.Millisecond.ToString() + exec;
                    //在指定的目录上保存上传的文件
                    fuConver.SaveAs(Server.MapPath("images/BKConver/" + newName));
                    ViewState["Conver"] = newName;
                }
                else
                {
                    Response.Write("<script>alert('文件格式不正确!请重新上传!');</script>");
                }
            }
            else
            {
                Response.Write("<script>alert('文件太大!上传失败!');</script>");
            }
        } 
    以上是上传图片的例子。自己套用一下就OK了。
      

  6.   

    ftpwebrequest上传文件
    FtpWebRequest req = (FtpWebRequest)WebRequest.Create("ftp://" + FtpAddress + FtpRemotePath + fi.Name);   
    req.Credentials = new NetworkCredential(FtpUid, FtpPwd);   fileupload选择文件实现上传
      

  7.   

    fileupload
    最简单、直接的。
      

  8.   


    string Name = FileUpload1.FileName;
            string lastName = null;
            if (Name.Trim().Length == 0)
            {
                //检查文件名是否为空
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择正确路径!')</script>");
            }
            else if ((lastName = Path.GetExtension(FileUpload1.FileName)) == ".vm")
            {
                //获取当前站点的物理路径
                //string directory = Server.MapPath("~/");            //随机生成文件名
                string fileName = Path.GetRandomFileName().Replace(".", "") + lastName;            //最终文件保存路径
                string path = Server.MapPath("~/") + @"Models_vm\" + fileName;            //文件保存至指定路径
                FileUpload1.SaveAs(path);
                Response.Write(path);
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交成功!')</script>");
            }
            else
            {
                this.Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('提交失败,文件类型不一致!')</script>");
            }
    恩,最近刚好写了个
      

  9.   

    我不能插入代码了,所有只有这样。没有语法高亮#region 事务操作
            /// <summary>
            /// 在上传文件时同时将数据存入数据库所做的事务操作,返回是否操作成功
            /// </summary>
            /// <param name="cmdText">insert命令</param>
            /// <param name="myParams">参数数组</param>
            /// <param name="file">文件上传组件</param>
            /// <param name="folderName">存放文件的路径</param>
            protected bool RunTranscation(string cmdText, OleDbParameter[] myParams, FileUpload file,string folderName)
            {
                string connectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["McmConnection"].ToString();//连接数据库字符串
                OleDbConnection conn = new OleDbConnection(connectionString);//新建连接
                conn.Open();//打开连接
                OleDbTransaction otran = conn.BeginTransaction();//开启事务
                OleDbCommand cmd = new OleDbCommand(cmdText, conn);//创建命令
                cmd.Transaction = otran;//设置命令的事物为otran            int result = 0;//判断插入数据库是否成功            //为cmd添加参数
                foreach (OleDbParameter param in myParams)
                {
                    cmd.Parameters.Add(param);
                }            try
                {
                    result=cmd.ExecuteNonQuery();//执行插入操作
                    if (result == 1)//如果插入成功
                    {
                        string fileName = file_pastPaper.FileName;
                        string filePath = "../Uploads/"+folderName;
                        file_pastPaper.SaveAs(Server.MapPath(filePath) + "/" + fileName);//上传到文件夹中
                        otran.Commit();//提交事务
                        return true;
                    }
                    otran.Rollback();//回滚事务,操作失败
                }
                catch (Exception se)
                {
                    otran.Rollback();//回滚事务
                    return false;
                }
                finally
                {
                    conn.Close();
                }            return false;
            }
            #endregion