码迷,mamicode.com
首页 > Web开发 > 详细

=L=图片上传整理

时间:2016-11-24 06:32:56      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:多个   exception   命名   controls   protected   文件格式   last   log   onclick   

前台:

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:FileUpload ID="fup" runat="server" />
        <br />
        <br />
        <asp:Button ID="btnUp" Text="上传图片" runat="server" OnClick="btnUp_Click" />
        <asp:Label id="lblInfo" runat="server" ForeColor="Red" Font-Size="13px" ></asp:Label>
        <br />
        <br />
        <asp:LinkButton ID="lbnPhoto" runat="server" PostBackUrl="~/Album/Photo.aspx" Text="查看照片"></asp:LinkButton>
    </div>
    </form>
</body>
</html>

后台:

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

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

    }


    private static bool IsAllowedExtension(FileUpload upfile)
    {
        string strOldFilePath = "";
        string strExtension = "";
        string[] arrExtension = { ".gif", ".jpg", ".bmp", ".png" };
        if (upfile.PostedFile.FileName != string.Empty)
        {
            strOldFilePath = upfile.PostedFile.FileName;//获得文件的完整路径名 
            strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));//获得文件的扩展名,如:.jpg 
            for (int i = 0; i < arrExtension.Length; i++)
            {
                if (strExtension.Equals(arrExtension[i]))
                {
                    return true;
                }
            }
        }
        return false;
    }
    //图片上传并将图片重命名
    protected void btnUp_Click(object sender, EventArgs e)
    {
        myClass myclass = new myClass();
         
        try
        {
            if (fup.PostedFile.FileName == "")
            {
                lblInfo.Text = "请选择文件!";
            }
            else
            {
                //string filepath = fup.PostedFile.FileName;
                if (!IsAllowedExtension(fup))
                {
                    lblInfo.Text = "上传文件格式不正确!";
                }
                if (IsAllowedExtension(fup) == true)
                {
                    string filepath = fup.PostedFile.FileName;
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    string serverpath = Server.MapPath("picture/") + filename;
                    fup.PostedFile.SaveAs(serverpath);
                    serverpath = "picture/" + filename;
                    DateTime now = DateTime.Now;
                    string sql = "insert into Photo (photoname,uptime,path)values(‘" + filename + "‘,‘" + now + "‘,‘" + serverpath + "‘)";
                    int flag = myclass.DataSQL(sql);
                    if (flag == 1)
                        lblInfo.Text = "上传成功!";
                    else
                        lblInfo.Text = "上传失败!";
                }
                else
                {
                    lblInfo.Text = "请上传图片!";
                }
            }
        }
        catch (Exception ex)
        {
            lblInfo.Text = "上传发生错误!原因是:" + ex.ToString();   
        }
    }


}

可以上传图片。限制图片类型。还缺少同时上传多个文件和限制图片大小。最后补充一下看了很久的注释_(:з」∠)_。发一下马克一下。说不定魔改完就不见了。。

=L=图片上传整理

标签:多个   exception   命名   controls   protected   文件格式   last   log   onclick   

原文地址:http://www.cnblogs.com/qingzhengxiangmingyue/p/6095568.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!