求一个文件上传的详细代码和注释。
需要上传文件到服务器指定文件夹,不能覆盖原有重名文件,并上传数个字段值(包括上传文件的路径和自定义字段值)到数据库,请各位大侠支招。(望保留注释,小弟学习)LANGUAGE="JAVASCRIPT"

解决方案 »

  1.   

    http://blog.csdn.net/zhoufoxcn/article/details/5765748
      

  2.   

    http://blog.csdn.net/zhoufoxcn/article/details/5772351
      

  3.   


     //上传文件
            public void FN_UpFiles()
            {
                //遍历File表单元素
                HttpFileCollection files = HttpContext.Current.Request.Files;
                //StringBuilder strMsg = new StringBuilder();
                //strMsg.Append("上传的文件分别是:<hr color='pink'/>");
                try
                {
                    for (int iFile = 0 ; iFile < files.Count ; iFile++)
                    {
                        //检查文件扩展名字
                        HttpPostedFile postedFile = files[iFile];
                        string fileName = "";
                        //string fileExtension = "";
                        fileName = Path.GetFileName(postedFile.FileName);
                        if (fileName != "")
                        {
                            try
                            {
                                string strpath = HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName;
                                if (System.IO.File.Exists(strpath))
                                {
                                    Response.Write("已经存在文件:" + fileName + "<br>");
                                }
                                else
                                {
                                    try
                                    {
                                        NRModel.File model = new NRModel.File();
                                        NRBLL.File bf = new NRBLL.File();
                                        Guid guid1 = Guid.NewGuid();
                                        Guid guid2 = Guid.NewGuid();
                                        Guid guid3 = Guid.NewGuid();
                                        Guid guid4 = Guid.NewGuid();
                                        model.Fileid = guid1;
                                        model.Folderid = guid2;
                                        model.Filepath = strpath;
                                        model.FileNam = fileName.ToString();
                                        model.FileSize = postedFile.ContentLength;
                                        model.Decription = this.decrition.Value;
                                        model.CreateOn = DateTime.Now;
                                        model.CreateBy = guid3;
                                        model.ModefyBy = guid4;
                                        if (bf.FN_AddNewRes(model) > 0)
                                        {
                                            //fileExtension = Path.GetExtension(fileName);
                                            //strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                                            //strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                                            //strMsg.Append("上传文件的文件名:" + fileName + "<br>");
                                            //strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
                                            //strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
                                            postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName);
                                            Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>");
                                            Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
                                            Response.Write("<script language='javascript'>window.close();</script>");
                                        }
                                    }
                                    catch (Exception ex)
                                    {                                    Response.Write(ex.ToString());
                                    }                            }                        }
                            catch (Exception ex)
                            {
                                Response.Write(ex.ToString());
                            }
                        }
                        else
                        {
                            Page.RegisterStartupScript("提示", "<script language='javascript'>alert('没有添加上传文件!')</script>");
                        }
                    }
                    //strStatus.Text = strMsg.ToString();
                }
                catch (System.Exception ex)
                {
                    Response.Write(ex.ToString());
                }
            }看一下不懂的问
      

  4.   

    汗 NRModel.File model = new NRModel.File();
                                        NRBLL.File bf = new NRBLL.File();
                                        Guid guid1 = Guid.NewGuid();
                                        Guid guid2 = Guid.NewGuid();
                                        Guid guid3 = Guid.NewGuid();
                                        Guid guid4 = Guid.NewGuid();
                                        model.Fileid = guid1;
                                        model.Folderid = guid2;
                                        model.Filepath = strpath;
                                        model.FileNam = fileName.ToString();
                                        model.FileSize = postedFile.ContentLength;
                                        model.Decription = this.decrition.Value;
                                        model.CreateOn = DateTime.Now;
                                        model.CreateBy = guid3;
                                        model.ModefyBy = guid4;
                                        if (bf.FN_AddNewRes(model) > 0)
                                        {
                                            //fileExtension = Path.GetExtension(fileName);
                                            //strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                                            //strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                                            //strMsg.Append("上传文件的文件名:" + fileName + "<br>");
                                            //strMsg.Append("上传文件的扩展名:" + fileExtension + "<br>");
                                            //strMsg.Append("上传文件的大小:" + postedFile.ContentLength.ToString() + "个字节" + "<br>");
                                            postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/ResourcesFolder/") + fileName);
                                            Page.RegisterStartupScript("提示", "<script language='javascript'>alert('上传成功!')</script>");
                                            Response.Write("<script language='javascript'>self.opener.location.reload();</script>");
                                            Response.Write("<script language='javascript'>window.close();</script>");
                                        }这个就是保存服务器文件路径到数据库的
      

  5.   

    楼主可以看看这个jquery插件
    http://www.cnblogs.com/oec2003/archive/2010/01/06/1640027.html
      

  6.   

    这个东西是好用,问题是不利于平台移植,我现在就是做个无脑版的,随时可以从一台服务器COPY到另外一台,啥都不用改的呵呵,所以只想要代码版的,插件版的暂时就不考虑了,不过还是谢谢楼上我再等等hebaobao19880921下午的回复吧,本来都做得差不多了,最后一个步骤总有点问题,可能脑筋一下没转过弯来进了死胡同,呵呵,所以求助一下,谢谢各位:)
      

  7.   

    model.Decription = this.decrition.Value;
    这个字段就是描述 你需要更多的描述 增加字段继续添加就可以了 如果你的意思只是说增加字段的问题的话
      

  8.   

    jquery 批量上传插件Uploadify
    参考