📄 fileupload.aspx.cs
字号:
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.Data.SqlClient;
using lalablog;
public partial class fileupload : System.Web.UI.Page
{
string randFileName;
string fullFileName;
int userid;
string add_ip;
int fileSize;
DateTime add_date;
int userTotalFileSize;
int perFileSize;
int totalFileSize;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["userid"] == null)
Response.Redirect("~/user/userLogin.aspx?url="+Server .HtmlEncode(Request .RawUrl ));
else userid = Convert.ToInt32(Session["userid"]);
writeSetting();
if (IsPostBack && FileUpload1.HasFile) uploadimg();
}
private string getUserTotalFileSize()
{
string sql = "select sum(picSize) from pic where userid=" + this.userid;
openDb open1 = new openDb(sql);
return open1.getValue();
}
private void writeSetting()
{
firstpage f1 = new firstpage();
f1.getSetting();
this.perFileSize = f1.perFileSize;
this.totalFileSize = f1.totalFileSize;
//获取用户已经使用的空间大小
string x = getUserTotalFileSize();
if (x != "")
this.userTotalFileSize = Convert.ToInt32(x);
else
this.userTotalFileSize = 0;
setting.InnerHtml = "允许上传的文件类型为 jpeg/jpg/png/gif";
setting.InnerHtml += "<br>允许上传的单个文件大小为" + perFileSize + "KB";
setting.InnerHtml += "<br>您还可以使用的空间为" + (totalFileSize * 1000 - userTotalFileSize) / 1000 + "kB";
}
private string uploadimg()
{
Boolean fileOK = false;
firstpage f1=new firstpage ();
f1.getSetting ();
int perFileSize = f1.perFileSize;
int totalFileSize = f1.totalFileSize;
string x = getUserTotalFileSize();
if (x != "")
this.userTotalFileSize = Convert.ToInt32(x);
else
this.userTotalFileSize = 0;
//检验是否指定后缀
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String path = Server.MapPath("~/Uploadfiles/");
randomTimeString rts = new randomTimeString();
this.randFileName = rts.str +fileExtension ;
this.fullFileName = path + randFileName;
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg"};
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
fileOK = true;
}
this.fileSize =FileUpload1.PostedFile.ContentLength;
//利用postedFile.ContentLength来判断文件长度,检查是否符合文件大小,250000就是250k字节
if (fileOK && this.fileSize <perFileSize*1000 )
fileOK = true;
else
fileOK = false;
if (fileOK && this.userTotalFileSize < totalFileSize * 1000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(this.fullFileName );
saveToDb();
System.Drawing.Image upimg = System.Drawing.Image.FromFile(this.fullFileName);
hfWidth .Value =upimg.Width.ToString ();
if (Convert.ToInt32(hfWidth.Value) > 400)
hfWidth.Value = "400";
string path = Request.ApplicationPath;
if (path != "/")
hfName.Value = path + "/uploadfiles/" + randFileName;
else
hfName.Value = "/uploadfiles/" + randFileName;
Image1.Visible = true;
Image1.ImageUrl = "../uploadfiles/" + randFileName;
Image1.Height = 150;
}
catch (Exception ex)
{
displayJavaMsg(ex.Message);
}
}
else
{
displayJavaMsg ("无法上传的文件类型或文件大小超过允许值");
}
return "";
}
private void saveToDb()
{
this.add_date = DateTime.Now;
this.add_ip = Request.UserHostAddress;
string sql = "insert into pic (userid,picName,upfilename,picSize,add_date,add_ip) ";
sql += "values(" + this.userid + ",'"+this.randFileName +"' ,'"+ this.randFileName + "'," + this.fileSize;
sql += ",'" + this.add_date + "','" + this.add_ip + "')";
openDb db1 = new openDb(sql);
string msg = db1.update();
//myfunc m1 = new myfunc(msg);
// Response.Write(m1.JavaMsg());
}
private void displayJavaMsg(string str)
{
Response .Write ("<div class=center><script type=text/javascript>alert('"+str +"');</script>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -