文件上传与下载

1.新建上传文件页面

文件上传与下载

FileUpload,button,4个label

代码
< body >
    
< form id = " form1 "  runat = " server " >
    
< div >
        
< asp:FileUpload ID = " FileUpload1 "  runat = " server "   />& nbsp; & nbsp;
        
< asp:Button ID = " Button1 "  runat = " server "
            Text
= " 上传文件 "  onclick = " Button1_Click "   />
        
< br  />
        
< br  />
        
< asp:Label ID = " Label4 "  runat = " server "  Text = " Label " ></ asp:Label >
        
< br  />
        文
& nbsp; 件 & nbsp; 名: < asp:Label ID = " Label1 "  runat = " server " ></ asp:Label >
        
< br  />
        文件类型:
< asp:Label ID = " Label2 "  runat = " server "  Text = "" ></ asp:Label >
        
< br  />
        文件大小:
< asp:Label ID = " Label3 "  runat = " server "  Text = "" ></ asp:Label >
        
< br  />
    
</ div >
    
</ form >
</ body >

 

双击按钮进行代码编写

代码
protected   void  Button1_Click( object  sender, EventArgs e)
        {
            
if  (FileUpload1.PostedFile  !=   null )
            {
                
string  nam  =  FileUpload1.FileName;
                
// 取得文件名(抱括路径)里最后一个"."的索引
                 int  i  =  nam.LastIndexOf( " . " );
                
// 取得文件扩展名
                 string  newext  =  nam.Substring(i);
                
// 这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
                DateTime now  =  DateTime.Now;
                
string  newname  = nam.Substring( 0 ,i)  +  now.Millisecond.ToString() +  FileUpload1.PostedFile.ContentLength;
                
// 保存文件到你所要的目录,这里是iis根目录下的upload目录.你能改动.
                
// 注意: 我这里用server.mappath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
                 string  mypath  =  Server.MapPath( " files " ) + " \\ "   +  newname  +  newext;
                
try
                {
                    FileUpload1.PostedFile.SaveAs(mypath);

                    Label1.Text 
=  newname + newext;
                    Label2.Text 
=  FileUpload1.PostedFile.ContentType;
                    Label3.Text 
=  FileUpload1.PostedFile.ContentLength.ToString() + " byte " ;
                   
//  Response.Write("<script>alert('上传失败!');</script>");
                    Label4.Text  =   " 上传成功 " ;
                }
                
catch  (Exception)
                {

                    
// Response.Write("<script>alert('上传成功!');</script>");
                    Label4.Text  =   " 上传失败 " ;
                }
                
// 得到这个文件的相关属性:文件名,文件类型,文件大小
                
            }

        }

2.下载页面:一个listbox 一个按钮

注意:需要引用 using System.IO;

代码
protected   void  Page_Load( object  sender, EventArgs e)
        {
            
if  ( ! IsPostBack)
            {
                bindListBox();
            }
        }
        
protected   void  bindListBox()
        {
            
// 将指定文件夹中的文件保存到字符串数组中
             string [] name  =  Directory.GetFiles(Server.MapPath( " files " ));
            
foreach  ( string  s  in  name)
            {
                
// 将文件名添加到ListBox中

                ListBox1.Items.Add(Path.GetFileName(s));
            }
        }
        
protected   void  Button1_Click( object  sender, EventArgs e)
        {
            
// 判断是否选择文件名
             if  (ListBox1.SelectedValue  !=   "" )
            {
                
if  (Session[ " file " ].ToString()  !=   "" )
                {   
// 获取文件路径
                     string  path  =  Server.MapPath( " files/ " +  Session[ " file " ].ToString();
                    
// 初始化 FileInfo 类的实例,它作为文件路径的包装
                    FileInfo fi  =   new  FileInfo(path);
                    
// 判断文件是否存在
                     if  (fi.Exists)
                    {
                        
// 将文件保存到本机上
                        Response.Clear();
                        Response.AddHeader(
" Content-Disposition " " attachment; filename= "   +  Server.UrlEncode(fi.Name));
                        Response.AddHeader(
" Content-Length " , fi.Length.ToString());
                        Response.ContentType 
=   " application/octet-stream " ;
                        Response.Filter.Close();
                        Response.WriteFile(fi.FullName);
                        Response.End();
                    }
                }
            }
            
else
            {
                Page.ClientScript.RegisterStartupScript(
this .GetType(), "" " <script>alert('请先选择文件名!')</script> " );
            }
        }

        
protected   void  ListBox1_SelectedIndexChanged( object  sender, EventArgs e)
        {
            Session[
" file " =  ListBox1.SelectedValue.ToString();
        }

 

文件上传与下载

 

你可能感兴趣的:(文件上传)