asp.net C#图片上传+预览

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.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
public partial class ImgUpdata : System.Web.UI.Page
     {
         protected void Page_Load(object sender, EventArgs e)
         {
             this.Image1.ImageUrl = "ImgUpload/null.gif";
             this.FileUpload1.Attributes.Add("onchange", "document.getElementById('Image1').src=document.getElementById('" + this.FileUpload1.ClientID + "').value;"); //图片预览
             //打开数据连接
             //SqlConnection cn = sqldata.createcon();
             //cn.Open();
         }
         protected void Button1_Click(object sender, EventArgs e)
         {
             bool yesno = false;
             bool rename = false;
             if (FileUpload1.HasFile)
             {
                 string fileContentType = FileUpload1.PostedFile.ContentType;
                 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                 {
                     string newname = "051011102";//session["userid"];
                     string name = FileUpload1.FileName;                   // 获取文件名称
                     string[] kzm = name.Split('.');
                     string webFilePath = Server.MapPath("ImgUpload/" + name);         // 服务器端文件路径
                     string newwebFilePath = Server.MapPath("ImgUpload/" + newname+"."+kzm[1]);
                     if (!File.Exists(webFilePath))
                     {
                         try
                         {
                             FileUpload1.SaveAs(webFilePath);                                 // 使用 SaveAs 方法保存文件
                             Label1.Text = "文件上传成功";
                             yesno = true;
                         }
                         catch (Exception ex)
                         {
                             Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                         }
                     }
                     else
                     {
                         Label1.Text = "提示:文件已经存在,请重命名后上传";
                     }
                     if (yesno == true)
                     {
                         try
                         {
                             File.Move(webFilePath, newwebFilePath);
                             rename = true;
                         }
                         catch (Exception ex)
                         {
                             Response.Write("<script>alert('文件上传错误')</script>");
                         }
                     }
                 }
                 else
                 {
                     Label1.Text = "提示:文件类型不符";
                 }
             }
             if (rename == true)
             {
                 //Response.Write("对了");
                 //updata数据库,并关闭数据库连接
                 //SqlCommand cmd = new SqlCommand("updata   ……", cn);
                 //cn.Close();
             }

         }
     
     }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值