zoukankan      html  css  js  c++  java
  • 文件上传的例子

    下面这个页面名字Upload.aspx 代码为

    %@ Page Language="C#" AutoEventWireup="true" CodeBehind="Upload.aspx.cs" Inherits="WebApplication1.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="fileUpload" runat="server" />
          <asp:Button ID="btnUpload" runat="server"  Text="上传" onclick="btnUpload_Click"/>
          <asp:Literal ID="literal" runat="server"></asp:Literal>
        </div>
        </form>
    </body>
    </html>

    后台CS代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    namespace WebApplication1
    {
        public partial class Upload : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
    
            protected void btnUpload_Click(object sender, EventArgs e)
            {
                //判断是否上传了文件
                if (fileUpload.HasFile)
                {
                    //指定上传文件在服务器上的保存路径
                    string savePath = Server.MapPath("~/upload/");
                    //检查服务器上是否存在这个物理路径,如果不存在创建
                    if (!System.IO.Directory.Exists(savePath))
                    {
                        //需要注意的是,对这个物理路径要有足够的权限,否则报错
                        //另外,这个路径应该在网站之下,而不是将网站部署在C盘
                        //把文件保存在D盘
                        System.IO.Directory.CreateDirectory(savePath);
                    }
                    savePath = savePath + "\" + fileUpload.FileName;
                    fileUpload.SaveAs(savePath);//保存在文件
                    //需要注意的是,在客户端访问指定的是URL地址,而不是在服务端
                    literal.Text = string.Format("<a href='upload/{0}'>upload/{0}</a>", fileUpload.FileName);
                }
            }
        }
    }
  • 相关阅读:
    欢迎加入【TIB自动化测试快讯】邮件列表!
    封装几个有用的QTP函数
    自动测试随想
    WatiN自动化测试
    使用vs2010的Coded UI Test实现数据驱动的用户自动登录测试
    QTP的DataTable操作
    TestComplete自动化测试课程大纲
    快速测试自动化
    自动化测试工程师职位(内部推荐)
    QTP库函数集
  • 原文地址:https://www.cnblogs.com/ai394495243/p/3339553.html
Copyright © 2011-2022 走看看