【转载】FileUpload控件如何实现文件上传

【转载】 FileUpload控件上传文件示例  

前台代码:添加FileUpload控件和Lable控件以及Button按钮控件


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="fileupload.aspx.cs" Inherits="fileupload" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title> FileUpload上传文件示例</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server" _disibledevent="Button1_Click" Text="上传文件" /><br />
        <asp:Label ID="Label1" runat="server" Height="269px" Text="Label" Width="360px"></asp:Label></div>

    </form>
</body>

</html>


aspx.cs:程序代码
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);
            Label1.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
                          "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
                          "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
                          "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" + 
                          "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
                          "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
        }
        catch (Exception ex)
        {
            Label1.Text = "发生错误:" + ex.Message.ToString();
        }
    }
    else
    {
        Label1.Text = "没有选择要上传的文件!";
    }


}

**********************华丽丽的分割线*******************************

---------------------------------------------------------------------------------------

1.一次上传多个文件

要一次上传多个文件,我们可以像传单个文件那样对每个文件单独进行处理,

除此之外,我们还可以使用HttpFileCollection类捕获从Request对象发送来的所有文件,然后再单独对每个文件进行处理,代码如下:

aspx.cs:程序代码
protected void Button1_Click(object sender, EventArgs e)
{
    string filepath = Server.MapPath("upload") + "\\";
    HttpFileCollection uploadFiles = Request.Files;
    for (int i = 0; i < uploadFiles.Count; i++)
    {
        HttpPostedFile postedFile = uploadFiles[i];
        try
        {
            if (postedFile.ContentLength > 0)
            {
                Label1.Text += "文件 #" + (i + 1) + ":" + System.IO.Path.GetFileName(postedFile.FileName) + "<br/>";
                postedFile.SaveAs(filepath + System.IO.Path.GetFileName( postedFile.FileName));
            }
        }
        catch (Exception Ex)
        {
            Label1.Text += "发生错误: " + Ex.Message;
        }
    }
}

2.上传文件类型的验证

对上传文件类型的验证既可以在客户端进行,也可以在服务器端进行。客户端可以使用验证控件来进行

不过我们今天主要说说如何在服务器端进行验证。上边cs文件中已经用GetExtension获取了文件的扩展名,只要稍加判断即可实现上传类型的验证:

aspx.cs:程序代码
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
        if (fileExt == ".rar" || fileExt == ".zip")
        {
            try
            {
                FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);
                Label1.Text = "客户端路径:" + FileUpload1.PostedFile.FileName + "<br>" +
                              "文件名:" + System.IO.Path.GetFileName(FileUpload1.FileName) + "<br>" +
                              "文件扩展名:" + System.IO.Path.GetExtension(FileUpload1.FileName) + "<br>" +
                              "文件大小:" + FileUpload1.PostedFile.ContentLength + " KB<br>" + 
                              "文件MIME类型:" + FileUpload1.PostedFile.ContentType + "<br>" +
                              "保存路径:" + Server.MapPath("upload") + "\\" + FileUpload1.FileName;
            }
            catch (Exception ex)
            {
                Label1.Text = "发生错误:" + ex.Message.ToString();
            }
        }
        else
        {
            Label1.Text = "只允许上传rar、zip文件!";
        }
    }
    else
    {
        Label1.Text = "没有选择要上传的文件!";
    }


}

需要注意的是,我们不能过分依赖于客户端验证控件和服务器端上述方法的验证,因为用户只需将文件扩展名更改为允许的类型就可以避开上边的验证,

这对用户来说并不是件困难的事情。

3.解决文件大小限制

在ASP.NET 2.0中FileUpload默认上传文件最大为4M,不过我们可以在web.cofig中修改相关节点来更改这个默认值,相关节点如下:


程序代码
<system.web>
    <httpRuntime maxRequestLength="40690" executionTimeout="6000" />
</system.web>


maxRequestLength表示可上传文件的最大值,executionTimeout表示ASP.NET关闭前允许发生的上载秒数。

4."multipart/form-data"和Request共存

在ASP程序中一旦使用表单上传文件(form的enctype属性值为multipart/form-data),服务器端就不能再用Request.Form来获取表单的值,这种限制在ASP.NET 2.0中已经不存在了:
aspx.cs:程序代码
protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile)
    {
        try
        {
            FileUpload1.SaveAs(Server.MapPath("upload") + "\\" + FileUpload1.FileName);
            Label1.Text = "上传文件:" + FileUpload1.FileName + "<br>" +
                          "说明:" + Request.Form["TextBox1"];//也可以用"TextBox1.Text"来获取说明
        }
        catch (Exception ex)
        {
            Label1.Text = "发生错误:" + ex.Message.ToString();
        }
    }
    else
    {
        Label1.Text = "没有选择要上传的文件!";
    }

}

本文转载于网易博客:

FileUpload控件上传文件示例 


net使用FileUpLoad控件上传文件
*********************************************************
【1】单个文件的上传:
保存到上传服务器 指定目录: FileUpload1.Save(Server.MapPath("/upfiles/upload/"+FileUpload1.FileName);
得到上传文件的 文件名( 含上传本地路径):FileUpload1.PostedFile.FileName;
得到上传文件的 大小:FileUpload1.PostedFile.ContentLength;
得到上传文件 上传类型:FileUpload1.PostedFile.ContentType;
得到上传文件 扩展名:System.IO.Path.GetExtension(FileUpload1.FileName);
得到上传 文件名:FileUpload1.FileName;
【2】同时多个文件的上传:
   方法是将 System.IO 类导入到 ASP.NET 页中,然后使用 HttpFileCollection 类捕获通过 Request 对象发送来的所有文件
该方法使您可以从一个页面上载所需数量的文件。
使用 HttpFileCollection 类和 Request.Files 属性使您可以控制从该页上载的所有文件。
(你可以在上传页面上放N个FileUpload控件)
得到上传的文件名:System.IO.Path.GetFileName(FileUpload1.FileName);
//Request.Files得到的多部分MIME格式的由客户端上载的文件的集合都是包含上传本地完整路径的。
protected void Button1_Click(object sender, EventArgs e)
{
   string filepath = Server.MapPath("/upfiles/upload/") ;
  HttpFileCollection uploadedFiles = Request.Files; 
for (int i = 0; i < uploadedFiles.Count; i++) 
   HttpPostedFile userPostedFile = uploadedFiles[i]; 
   try 
   { 
      if (userPostedFile.ContentLength > 0 ) 
      { 
           Label1.Text += "File #" + (i+1) + ""; 
           Label1.Text += "File Content Type: " + userPostedFile. ContentType + ""; 
           Label1.Text += "File Size: " + userPostedFile. ContentLength + "kb";
           Label1.Text += "File Name: " + userPostedFile. FileName + ""; 
           userPostedFile.SaveAs(filepath + "\\" + System.IO.Path. GetFileName(userPostedFile.FileName)); 
           Label1.Text += "Location where saved: " + filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName) + ""; }
  } 
  catch (Exception Ex) 
  { 
       Label1.Text += "Error: " + Ex.Message; }
  } 

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值