用FileUpload控件上传文件

来源:互联网 发布:ppt软件为什么打不开 编辑:程序博客网 时间:2024/06/11 23:50

一、upload.aspx

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upload.aspx.cs" Inherits="upload" %>

<!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></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" /><br />
        <asp:FileUpload ID="FileUpload2" runat="server" /><br />
        <asp:FileUpload ID="FileUpload3" runat="server" /><br />
        <br /><br />
        <asp:Button ID="btnOK" runat="server" Text="确定" onclick="btnOK_Click" />
    </div>
    </form>
</body>
</html>

 

二、upload.aspx.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class upload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        #region
        //上传一个文件
        //string fullfilename = this.FileUpload1.PostedFile.FileName;//.PostedFile可以去掉
        //string filename = fullfilename.Substring(fullfilename.LastIndexOf("\\") + 1);
        //this.FileUpload1.PostedFile.SaveAs(Server.MapPath("files") + "\\" + filename);//.PostedFile可以去掉
        #endregion
        #region
        //上传一个文件
        //string path = Server.MapPath("files");
        //FileUpload1.SaveAs(path + @"\" + FileUpload1.FileName);
        #endregion
        #region
        //上传多个文件
        string path = Server.MapPath("files");
        HttpFileCollection hfc = Request.Files;//接收从客户端传递过来的数据
        for (int i = 0; i < hfc.Count; i++)
        {
            if (hfc[i].ContentLength > 0)//获取上传文件的大小
            {
                hfc[i].SaveAs(path + @"\" + hfc[i].FileName);
            }
        }
        #endregion
    }
}

 


 

原创粉丝点击