zoukankan      html  css  js  c++  java
  • 文件上传两种方式

     一种方式

    /// <summary>

        /// 上传文件
        
    /// </summary>
        
    /// <param name="filepathLevel">上传文件的路径层</param>
        
    /// <param name="filepath">根目录下文件存在的路径</param>
        
    /// <param name="filename">文件名称,默认null</param>
        
    /// <param name="fileFormat">文件格式(以,分隔)</param>
        
    /// <param name="maxAmount">文件大小(MB计算)</param>
        
    /// <param name="fud">上传组件名</param>
        
    /// <param name="fileIsExists">是否必需上传</param>
        
    /// <returns></returns>
        public static string FileUpload(string filepathLevel, string filepath, string filename, string fileFormat, int maxAmount, FileUpload fud,bool fileIsExists)
        {
            
    bool formatStatus = false;
            
    string eorrStr = "";
            
    string strText = null;
            
    string tmpfilepath = System.Web.HttpContext.Current.Server.MapPath(filepathLevel) + filepath;
            
    string tmpfilename;
            
    if (filename == null)   ///默认名称
            { tmpfilename = DateTime.Now.ToString("yyyyMMddhhmmss"+ DateTime.Now.Millisecond.ToString(); }
            
    else
            { tmpfilename 
    = filename; }
            
    ///---------------------------创建文件夹---------------------------
            //HttpContext.Current.Server.MapPath(相对路径):把相对路径转为服务器上的绝对路径。File.Exists(绝对路径):检查是否存在绝对路径指向的文件或目录。
            if (!File.Exists(tmpfilepath))
            {
                System.IO.Directory.CreateDirectory(tmpfilepath);              
    //System.IO.Directory.CreateDirectory(文件夹绝对路径):建立绝对路径文件夹。
            }
            
            
    ///------------------------------写入上传文件--------------------------------
            if (fud.HasFile)
            {
                
    try
                {
                    
    string[] strTpye = fud.FileName.Split('.');
                    
    if (strTpye.Length > 1)
                    {  
                        
    int i_split = strTpye.Length - 1;
                        
    string format = strTpye[i_split];
                        
    string[] sArray = fileFormat.Split(',');
                        
    foreach (string i in sArray)
                        {
                            
    if (format.ToLower() == i.ToString())
                             {                            
                                formatStatus 
    = true;
                                
    break;
                            }
                        }
                        
    ///------------------判断文件格式--------------------
                        if (formatStatus == false)   
                        {
                            eorrStr 
    += "只能上传" + fileFormat + "格式文件";
                        }
                        
    ///------------------判断大小--------------------
                        int tmpmaxAmount = maxAmount * 1024 * 1024;
                        
    if (fud.PostedFile.ContentLength > tmpmaxAmount)  
                        {
                            eorrStr 
    += "上传不能超过" + maxAmount + "MB";
                        }
                        
    ///------------------写入文件--------------------
                        if (eorrStr == "")
                        {
                            
    string savepath = tmpfilepath + tmpfilename + "." + format;
                            
    if (!File.Exists(savepath))   //不存在就写入
                            {
                                fud.SaveAs(tmpfilepath 
    + tmpfilename + "." + format);
                            }                         
                            strText 
    = filepath + tmpfilename + "." + format;    
                        }
                    }
                    
    else
                    {
                        eorrStr 
    += "只能上传" + fileFormat + "格式文件";
                    }
                    
    //  显示文件信息
                    
    //str += "<br/>Saved As: " + FileUpload1.PostedFile.FileName;
                    
    //str += "<br/>File Type: " + FileUpload1.PostedFile.ContentType;
                    
    //str += "<br/>File Length (bytes): " + FileUpload1.PostedFile.ContentLength;
                    
    //str += "<br/>PostedFile File Name: " + FileUpload1.PostedFile.FileName;
                    
    // fud.SaveAs(filepath + FileUpload1.FileName);
                    
    //str += "Uploading file: " + FileUpload1.FileNam
                }
                
    catch (Exception ex)
                {
                    eorrStr 
    += "<b>Error:</b>Unable to save :" + fud.FileName + "<br/>" + ex.Message;
                }
            }
            
    else
            {
                
    if (fileIsExists == true)  ///必需上传文件
                {
                    eorrStr 
    = "No file uploaded.";
                }
            }

            
    if (eorrStr != "")
            { JScript.Alert(eorrStr); }
            
    return strText;        
        }

      

     二种方式

    //upload.aspx

    <form name="form1" action="Save.aspx" method="post" enctype="multipart/form-data">
    <INPUT type="file" id="File1" name="File1">
    <input type="submit" name="submit" value="保存">
    </form>


      
    private void Page_Load(object sender, System.EventArgs e)
      {
       
    if( Request.Files.Count == 0 ) {
        Response.Write(
    "<script>alert('请选择上传文件!');</script>");
        Response.End();
        
    return;
       }
       System.Web.HttpPostedFile file 
    = Request.Files[0];
       String fileName 
    = Request["fileName"];
       String save_path 
    = "./attached/";
       String ext 
    = System.IO.Path.GetExtension( file.FileName ).ToLower();
       Response.Write(ext);
       
    if!System.IO.Directory.Exists( Server.MapPath( save_path ) ) )
        System.IO.Directory.CreateDirectory( Server.MapPath( save_path ) );
       
    if( ext == ".jpg" || ext == ".gif" || ext == ".bmp" || ext == ".png" ) {
        file.SaveAs( Server.MapPath( save_path 
    + fileName ) );
        Response.Write(
    "<script type=""text/javascript"">parent.KindInsertImage('" + save_path + fileName + "','" + Request["imgWidth"+ "','" + Request["imgHeight"+ "','" + Request["imgBorder"+ "');</script>'");
        Response.End();
       }
       
    else {
        Response.Write(
    "<script>alert('文件格式不支持!');</script>");
        Response.End();
       }
      }

  • 相关阅读:
    读取lotus85邮箱未读文档
    Lotus notes 通讯录的导入导出
    如果你发现vpuserinfo.nsf数据库损坏,如何修复?该数据
    如何从iNotes获得未读邮件数量
    NTKO文档控件常见报错信息集合
    页面缓存清除的方法
    通过Lotusscript代码从损坏的数据库中抽取数据
    如何将lotus 通讯簿导入到outlook 2003中
    怎样使用Lotus Domino实用程序(Updall, Compact, Fixup) 进行正常和异常维护。
    0301.Lotus Domino与Windows AD帐户同步和SSO
  • 原文地址:https://www.cnblogs.com/qgf522/p/1275550.html
Copyright © 2011-2022 走看看