图片片上传前的校验处理

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadImage.aspx.cs" Inherits="UploadImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>UpFile</title>
  <meta name="GENERATOR" Content="Microsoft FrontPage 6.0">
  <LINK href="../css/css.css" type="text/css" rel="stylesheet">
  <META http-equiv="Content-Type" content="text/html; charset=GB2312">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <script language="javascript">
      function CheckRadio(namevalue) {

        if (namevalue=="Radio2")
         {
          document.Form1.FileUpload1.disabled=false;   
          document.Form1.urlname.readOnly=true; 
         }
         else
         {
          document.Form1.FileUpload1.disabled=true;   
          document.Form1.urlname.readOnly=false; 
         }
       } 
     function PreviewImage(str)
     {
                  if (str=="urlname")
         {
                     document.getElementById("imgID").src='file:///'+str;  
         }
         else
         {
                        document.getElementById("imgID").src=str;  
         }
          
     }
  </script>
 </HEAD>
 <body topmargin="0" leftmargin="0">
  <form id="Form1" name="Form1" method="post" runat="server">
   <table border="0" width="100%" id="table2" style="BORDER-COLLAPSE: collapse" height="194">
    
    <TR>
     <TD align="left" height="43" width="100%">&nbsp;&nbsp;&nbsp;&nbsp;
      <asp:label id="lblhead" Width="96px" Height="18px" Runat="server">缩略图:</asp:label>
      <asp:Image id="imgID" runat="server" Height="150px" Width="200px"></asp:Image>
      <asp:Label id="UpLoadmsg" runat="server"></asp:Label>
      <table border="0" width="100%" id="table3">
       <tr>
        <td height="19" width="60%">
         <input type="radio" runat="server" οnclick="CheckRadio(this.id)"  name="R1" id="Radio2" checked>上传产品图片:
                                    <asp:FileUpload ID="FileUpload1" runat="server" Width="291px" onpropertychange="PreviewImage(this.value);"/></td>
       </tr>
       
       <tr>
        <td width="60%">
         <input type="radio" runat="server" οnclick="CheckRadio(this.id)"  name="R1" id="Radio1">图片所在网址:<FONT face="宋体">
                                        <asp:TextBox ID="urlname" runat="server" Width="284px" onpropertychange="PreviewImage(this.value);">http://</asp:TextBox></FONT></td>
       </tr>
       <tr>
        <td width="60%" height="10"></td>
       </tr>
       <tr>
        <td width="60%" align="center">
                                        <table width="100%">
                                            <tr>
                                                <td align="center" style="width: 144px">
                     <asp:button id="BtnUP" runat="server" Width="43px" CSSclass="button1" text="上传" οnclick="BtnUP_Click"></asp:button></td>
                                                <td style="width: 100px">
                                                </td>
                                            </tr>
                                        </table>
        </td>
       </tr>
      </table>
     </TD>
    </TR>
    </table>
  </form>
 </body>
</HTML>

 

 

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class UploadImage : System.Web.UI.Page
{
   
    protected void Page_Load(object sender, EventArgs e)
    {
        // 检查权限
    }
    #region 上传文件
    protected void BtnUP_Click(object sender, System.EventArgs e)
    {
        UpLoadImageData FileUP = new UpLoadImageData();
        try
        {
            if (Radio1.Checked == true)
            {
                FileUP.ReadNetImage(this, this.Request.Params["urlname"].ToString(), "MapPathstr", "URLType", "ProID", "NOTSml");
            }
            else
            {
                string signStr = CheckFile();
                if ( signStr == "成功")
                {
                    FileUP.ReadNativeImage(this, FileUpload1, "MapPathstr", "URLType", "ProID", "NOTSml");
                }
                else
                {
                    UpLoadmsg.Text = signStr;
                }
            }
          Response.Write("上传完成");
        }
        catch (Exception)
        {
            UpLoadmsg.Text ="上传失败";
        }
      
    }   
    #endregion

    #region 检查文件是否符合条件
    protected string  CheckFile()
    {
        string extension = Path.GetExtension(FileUpload1.FileName).ToLower();
        if (!FileUpload1.HasFile)
        {
            return "请输入要上传的文件!";
        }
        else
        {
            if (extension != ".jpg" && extension != ".png" && extension != ".gif")
            {
                return "对不起,你所上传的文件类型不对!";
            }
            else
            {
                if (FileUpload1.PostedFile.ContentLength / 1024 / 1024 > 2)
                {
                    return "对不起,你所上传的文件不能超过的2M。文件大小!";
                }
                else
                {
                    return "成功";
                }
            }
        }
    }
    #endregion

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值