📄 upfile.aspx.cs
字号:
using System;
using System.Data;
using System.Data.SqlClient;
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;
public partial class Upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["UserID"] == null)
Response.Redirect("index.aspx");
Imageview.Visible = false;
}
protected void Upbtn_Click(object sender, EventArgs e)
{
// this.RegularExpressionValidator1.Enabled = false;
bool fileOK = false;
//获取根文件绝对路径
string path = Server.MapPath("~/UpLoad/");
//如上传了文件,就判断文件格式
FileUpload FU = FileUpload1;
if (FU.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FU.FileName).ToLower();
string[] allowedExtensions ={ ".gif", ".jpg", ".png", ".bmp", };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
if (fileOK)
{
//图片大小限制为 1024K
if (FU.PostedFile.ContentLength > 1024000)
{
RegularExpressionValidator req = ((RegularExpressionValidator)Maintab.FindControl("RegularExpressionValidator1"));
req.ErrorMessage = "请把图片大小保持在1024K以内。";
req.IsValid = false;
fileOK = false;
}
}
//调用saveas方法,实现上传文件
}
if (fileOK)
{
this.RegularExpressionValidator1.Enabled = false;
try
{
string picpath;
Imageview.Visible = true;
picpath = System.DateTime.Now.ToString("yyyyMMddhhmmss");
FileUpload1.SaveAs(path + picpath + FU.FileName);
Imageview.ImageUrl = "~/Upload/" + picpath + FU.FileName;
// picpath = "~/Upload/" + System.DateTime.Now.ToString("yyyyMMddhhmmss") + FU.FileName;
Upbtn.Text = "上传成功";
//CREATE PROCEDURE [Upimg]
//(
// @Title nchar(60),
//@Fromclass nchar(20),
//@FromUserID char(20),
//@picofsch char(10),
//@picofper char(10),
//@picofcla char(10)
//)
// AS insert into [PicInfo]
//(
// Title ,
// Fromclass ,
// FromUserID ,
// picofsch ,
// picofper ,
// picofcla
// )
//VALUES
//(
//@Title ,
//@Fromclass ,
//@FromUserID ,
//@picofsch ,
//@picofper ,
//@picofcla
// )
//GO
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"]);
SqlCommand myCommand = new SqlCommand("Upimg", conn);
myCommand.CommandType = CommandType.StoredProcedure;
//store
SqlParameter parTitle = new SqlParameter("@Title", SqlDbType.NChar, 60);
parTitle.Value = picpath + FU.FileName;
myCommand.Parameters.Add(parTitle);
SqlParameter parFromclass = new SqlParameter("@Fromclass", SqlDbType.NChar, 20);
parFromclass.Value = "";
myCommand.Parameters.Add(parFromclass);
SqlParameter parFromUserID = new SqlParameter("@UserID", SqlDbType.NChar, 30);
parFromUserID.Value = Session["UserID"].ToString();
myCommand.Parameters.Add(parFromUserID);
SqlParameter parpicofsch = new SqlParameter("@picofsch", SqlDbType.Char, 10 );
parpicofsch.Value = "2";
myCommand.Parameters.Add(parpicofsch);
SqlParameter parpicofper = new SqlParameter("@picofper", SqlDbType.Char, 10);
parpicofper.Value = '1';
myCommand.Parameters.Add(parpicofper);
SqlParameter parpicofcla = new SqlParameter("@picofcla", SqlDbType.Char, 10);
parpicofcla.Value = '2';
myCommand.Parameters.Add(parpicofcla);
conn.Open();
myCommand.ExecuteNonQuery();
conn.Close();
}
finally
{
}
}
else
{
Upbtn.Text = "上传失败,格式不允许";
}
this.RegularExpressionValidator1.Enabled = true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -