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>
  • 相关阅读:
    按照两种模式排序用户列表
    WINDOWS系统属性符号说明
    一个无敌删除命令
    SQL判断某列中是否包含中文字符、英文字符、纯数字
    泰勒公式的发现以及证明
    陶哲轩实分析 引理7.1.4 证明
    多项式函数在某一点处的泰勒展开
    域上多项式的带余除法
    陶哲轩实分析 命题7.18 证明
    陶哲轩实分析 引理7.1.4 证明
  • 原文地址:https://www.cnblogs.com/zzxap/p/2176012.html
Copyright © 2011-2022 走看看