zoukankan      html  css  js  c++  java
  • ASP.NET上传图片文件

    using System;
    using System.IO;
    public partial class _Default : System.Web.UI.Page
    {
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            if(IsPostBack)
            {
                Boolean fileOK = false;//定义变量判断文件是否为图片
                String path = Server.MapPath("~/UploadedImages/");//指定文件保存路径
                if (fuUpload.HasFile) //判断控件是否选择文件
                {
                    String fileExtension = Path.GetExtension(fuUpload.FileName).ToLower();//使用该方法得到上传文件的扩展名
                    String[] allowedExtensions = {".gif", ".png", ".jpeg", ".jpg"};//定义允许上传的扩展名
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                       if (fileExtension == allowedExtensions[i])//如果上传文件通过检测
                       {
                            fileOK = true;//将该变量设值为true
                       }
                    }
                }
                if (fileOK)//判断是否通过检测
                {
                    try
                    {
                        if (!Directory.Exists(path))//使用Directory类的方法判断保存路径目录是否存在
                        {
                            Directory.CreateDirectory(path);//如果不存在便创建该目录
                        }
                        fuUpload.PostedFile.SaveAs(path + fuUpload.FileName);
                        Label1.Text = "File uploaded!";
                    }
                    catch(Exception ex)
                    {
                        Label1.Text = "File could not be uploaded." + ex.Message;
                    }
                }
                else
                {
                    Label1.Text = "Cannot accept files of this type.";
                }
            }
            string strFileName = fuUpload.FileName;
        }
    }

    HTML_Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
    <!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="fuUpload" runat="server" Height="24px" Style="z-index: 100;
                left: 104px; position: absolute; top: 84px" Width="480px" />
            <asp:Label ID="Label1" runat="server" Style="z-index: 103; left: 104px; position: absolute;
                top: 52px" Width="476px"></asp:Label>
            <asp:Button ID="btnUpload" runat="server" Height="24px" OnClick="btnUpload_Click"
                Style="z-index: 102; left: 104px; position: absolute; top: 124px" Text="上传" Width="84px" />
           
        </div>
        </form>
    </body>
    </html>
  • 相关阅读:
    我的本科毕业论文——Messar即时通讯系统
    你为什么不用Flash做程序的表示层呢?
    用于Blog的天气预报服务-改进20050806
    写了个小程序,方便大家编程(QuickDog,快捷键帮手)
    庆祝"上海.NET俱乐部"今天成立,请申请加入的朋友在这里Sign you name
    HTML+CSS+Javascript教学视频【0409更新】
    关于推迟7月9日上海.NET俱乐部第一次技术交流会的通知
    关于“上海.NET俱乐部”第一次技术交流会进展报告
    2005年8月13日 上海.NET俱乐部第一次活动纪实 已经发布,资料提供下载
    喜欢互联网行业,是因为它拥有着无穷的变数
  • 原文地址:https://www.cnblogs.com/zzxap/p/2176012.html
Copyright © 2011-2022 走看看