⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uploadfile.aspx.cs

📁 计算机学院网站及管理系统
💻 CS
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;

namespace ComputerWeb
{
	/// <summary>
	/// Summary description for UploadFile.
	/// </summary>
	public class UploadFile : System.Web.UI.Page
	{
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton FileType;
		protected System.Web.UI.HtmlControls.HtmlInputRadioButton PictureType;
		protected System.Web.UI.WebControls.ListBox FileList;
		protected System.Web.UI.WebControls.Button SurBtn;
		protected System.Web.UI.WebControls.TextBox Description;
		protected System.Web.UI.WebControls.Button AddFileBtn;
		protected System.Web.UI.HtmlControls.HtmlInputFile LoadFile;

		private static int nFlag = 0;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			if(!Page.IsPostBack)
			{
				nFlag      = 0;
				FileList.Items.Clear();
			}
		}

		/// <summary>
		/// 上传图片
		/// </summary>
		private int AddPicture()
		{
			int nPictureID = 0;
			Stream pictureStream = LoadFile.PostedFile.InputStream;

			//图片具体数据
			byte[] pictureContent = new byte[LoadFile.PostedFile.ContentLength];

			//读取图片的文件流
			pictureStream.Read(pictureContent,0,LoadFile.PostedFile.ContentLength);			

			PictureDB picture = new PictureDB();
			if(LoadFile.Value.ToString() != "")
			{			
				try
				{
					nPictureID = picture.AddPicture(Description.Text,pictureContent,0,LoadFile.PostedFile.ContentType,"0");
				}
				catch(Exception ex)
				{
					string sRawURL = Request.RawUrl;

					if(sRawURL.IndexOf("?") > -1)
					{
						sRawURL = sRawURL.Substring(0,sRawURL.IndexOf("?"));
					}
					Response.Redirect("~/Admin/AdminDenid.aspx?ErrorURL=" + sRawURL + "&ErrorInfo=" + ex.Message.Replace("\n","<br>"));
				}				
			}
			else
			{
				Response.Write("<script>alert(\"你还没有上载图片!\")</script>");
			}

			return(nPictureID);
		}
		
		private int AddFile()
		{
			int nFileID = 0;
			PictureDB picture = new PictureDB();

			if((Description.Text.Trim() != "")&&(LoadFile.PostedFile.ContentLength > 0))
			{  
				String fileName = LoadFile.PostedFile.FileName.Substring(LoadFile.PostedFile.FileName.LastIndexOf("\\"),
					LoadFile.PostedFile.FileName.Length - LoadFile.PostedFile.FileName.LastIndexOf("\\"));

				String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() 
					+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() 
					+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
					+ DateTime.Now.Millisecond.ToString();

				fileName = "\\" + fileTime + GetRandomint() + fileName.Substring(fileName.IndexOf("."),fileName.Length - fileName.IndexOf("."));
					
				if(File.Exists(Server.MapPath(Request.ApplicationPath) + "\\upLoad" + fileName) == false)
				{
					//把文件存入磁盘,如果失败,导向提示页面
					try
					{
						LoadFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\upLoad" + fileName);
					}
					catch  (Exception ex)
					{
						string sRawURL = Request.RawUrl;

						if(sRawURL.IndexOf("?") > -1)
						{
							sRawURL = sRawURL.Substring(0,sRawURL.IndexOf("?"));
						}
						Response.Redirect("~/Admin/AdminDenid.aspx?ErrorURL=" + sRawURL + "&ErrorInfo=" + ex.Message.Replace("\n","<br>"));							
					}

					//把文件路径和描述存入数据库,如果失败,导向提示页面
					try
					{
						nFileID = picture.AddPicture(Description.Text  + "(" 
							+ fileName.Substring(fileName.IndexOf(".") + 1,fileName.Length - fileName.IndexOf(".") -1) + ")",
							null,0,LoadFile.PostedFile.ContentType,"\\upLoad" + fileName);
						
					}
					catch(Exception ex)
					{
						//如果数据库异常,将文件从磁盘上删除,以保持数据的一致性。
						File.Delete(Server.MapPath(Request.ApplicationPath) + "\\upLoad" + fileName);
						string sRawURL = Request.RawUrl;

						if(sRawURL.IndexOf("?") > -1)
						{
							sRawURL = sRawURL.Substring(0,sRawURL.IndexOf("?"));
						}
						Response.Redirect("~/Admin/AdminDenid.aspx?ErrorURL=" + sRawURL + "&ErrorInfo=" + ex.Message.Replace("\n",""));
					}
				}
				else
				{
					Response.Write("<script>alert(\"你上传的文件已经存在!\")</script>");
				}
			}
			else
			{
				Response.Write("<script>alert(\"你输入的文件描述\\文件名为空,请重新输入!\")</script>");
			}

			return(nFileID);
		}

		private String GetRandomint()
		{
			Random random = new Random();

			return(random.Next(10000).ToString());
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.AddFileBtn.Click += new System.EventHandler(this.AddFileBtn_Click);
			this.SurBtn.Click += new System.EventHandler(this.SurBtn_Click);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void AddFileBtn_Click(object sender, System.EventArgs e)
		{
			if(PictureType.Checked == true)
			{
				FileList.Items.Add(new ListItem(Description.Text,AddPicture().ToString()));

				//清空以上传图片的说明
				Description.Text = "";
				if(nFlag > 1)
				{
					nFlag = 3;
				}
				else
				{
					nFlag = 1;
				}
			}
			else
			{
				if(FileType.Checked == true)
				{
					FileList.Items.Add(new ListItem(Description.Text,AddFile().ToString()));

					//清空以上传图片的说明
					Description.Text = "";
					if(nFlag == 1)
					{
						nFlag = 3;
					}
					else
					{
						nFlag = 2;
					}
				}
			}
		}

		private void SurBtn_Click(object sender, System.EventArgs e)
		{
			GlobalVars.FilePictureID.Clear();

			for(int i = 0; i < FileList.Items.Count; i++)
			{
				GlobalVars.FilePictureID.Add(FileList.Items[i].Value);
			}

			GlobalVars.FilePictureID.Add(nFlag.ToString());

			Response.Write("<script>window.close();</script>");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -