• 多文件上传


    第一种方法
     
     //上传文件
            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());
                }
     
    第二种方法
     
    public partial class _Default : System.Web.UI.Page  
        protected void Page_Load(object sender, EventArgs e) 
        
     
        
        protected void bt_upload_Click(object sender, EventArgs e) 
        
            if (FileUpload1.PostedFile.FileName == "" && FileUpload2.PostedFile.FileName == "" && FileUpload3.PostedFile.FileName == ""
            
                this.lb_info.Text = "请选择文件!"
            
            else 
            
                HttpFileCollection myfiles = Request.Files; 
                for (int i = 0; i < myfiles.Count; i++) 
                
                    HttpPostedFile mypost = myfiles[i]; 
                    try 
                    
                        if (mypost.ContentLength > 0) 
                        
                            string filepath = mypost.FileName;//C:\Documents and Settings\Administrator\My Documents\My Pictures\20022775_m.jpg 
                            string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);//20022775_m.jpg 
                            string serverpath = Server.MapPath("~/images/") + filename;//C:\Inetpub\wwwroot\WebSite2\images\20022775_m.jpg 
                            mypost.SaveAs(serverpath); 
                            this.lb_info.Text = "上传成功!"
                        
                    
                    catch (Exception ex) 
                    
                        this.lb_info.Text = "上传发生错误!原因:" + ex.Message.ToString(); 
                    
                
            
        
    }
     
     
  • 相关阅读:
    KMeans算法分析以及实现
    决策树(ID3,C4.5,CART)原理以及实现
    [推荐系统读书笔记]利用用户标签数据
    [推荐系统读书笔记]推荐系统冷启动问题
    [推荐系统]利用用户行为数据
    [推荐系统读书笔记]好的推荐系统
    Docker Hub国内镜像加速
    ubuntu下cannot connect to X server :1
    vscode编写C++设置左花括号不换行
    SLAM十四讲中Sophus库安装
  • 原文地址:https://www.cnblogs.com/tiancai/p/2936940.html
Copyright © 2020-2023  润新知