asp.net 上传文件

来源:互联网 发布:angular.js 2.0下载 编辑:程序博客网 时间:2024/05/17 16:01
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Add_UpdateSoftVersion.aspx.cs" Inherits="WebApp.Add_UpdateSoftVersion" %><!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>    <script type="text/javascript" language="javascript">        function addForm() {            var strForm = "<input type='file' size='50' name='File' /><br/>"            document.getElementById('MyFile').insertAdjacentHTML("beforeEnd", strForm)        }    </script></head><body>    <form id="form1" runat="server" enctype="multipart/form-data">    <div>            <table style="width:100%;">            <tr>                <td>                    <asp:Label ID="Label2" runat="server" Text="设备类型:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_DeviceTypeId" runat="server"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label4" runat="server" Text="文件类型:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_FileTypeId" runat="server"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label8" runat="server" Text="软件主版本编号:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_SoftVersion" runat="server"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label10" runat="server" Text="次版本编号:"></asp:Label>                </td>                <td>                    <asp:DropDownList ID="ddl_VersionType" runat="server">                        <asp:ListItem Value="0">A</asp:ListItem>                        <asp:ListItem Value="1">B</asp:ListItem>                    </asp:DropDownList>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label5" runat="server" Text="添加时间:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_AddTime" runat="server" Enabled="False"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label6" runat="server" Text="录入人员:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_UserId" runat="server" Enabled="False"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label9" runat="server" Text="文件名:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_FileName" runat="server"></asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                    <asp:Label ID="Label7" runat="server" Text="成功升级次数:"></asp:Label>                </td>                <td>                    <asp:TextBox ID="txt_UpdateCount" runat="server" Enabled="False">0</asp:TextBox>                </td>                <td>                     </td>            </tr>            <tr>                <td>                     </td>                <td>                    <asp:Button ID="btnAdd" runat="server" Height="35px" onclick="btnAdd_Click"                         Text="添加" Width="81px" />                </td>                <td>                     </td>            </tr>        </table>    </div>    <div>       <p id="MyFile">          <input type="file" size="50" name="File" />    </p>    <p>          <input type="button" value="增加一个" onclick="addForm()" />                    <asp:Button Runat="server" Text="开始上传" ID="UploadButton"                 onclick="UploadButton_Click">          </asp:Button>                    <input onclick="this.form.reset()" type="button" value="重 置" />          <br />          <asp:Label ID="Label1" runat="server"></asp:Label>    </p>    </div>    </form></body></html>
  protected void UploadButton_Click(object sender, EventArgs e)        {            SaveFiles();        }        private void SaveFiles()        {            //遍历表单元素            HttpFileCollection files = HttpContext.Current.Request.Files;            //状态信息            string strout = "<br>上传的文件分别是:<hr color=red><table style='width: 500px;'>";            strout += "<tr><td>文件类型</td><td>客户端地址</td><td>上传文件名</td><td>扩展名</td></tr>";            try            {                for (int iFile = 0; iFile < files.Count; iFile++)                {                    //访问单独文件                    HttpPostedFile postedFile = files[iFile];                    string fileName, fileExtension;                    fileName = System.IO.Path.GetFileName(postedFile.FileName);                    if (fileName != "")                    {                        fileExtension = System.IO.Path.GetExtension(fileName);                        strout += "<tr><td>" + postedFile.ContentType.ToString() + "</td>";                        strout += "<td>" + postedFile.FileName + "</td>";                        strout += "<td>" + fileName + "</td>";                        strout += "<td>" + fileExtension + "</td></tr>";                        postedFile.SaveAs(Server.MapPath("DtuUpdateFile/") + fileName);                    }                    else                    {                        strout = "<br>请您选择一个文件!!!";                    }                }                Label1.Text = strout.ToString();            }            catch (Exception Ex)            {                Label1.Text = Ex.ToString();            }        }


原创粉丝点击