?? upfile.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 lalablog;
public partial class edit_upfile : System.Web.UI.Page
{
string randFileName;
string fullFileName;
int userid;
string add_ip;
int fileSize;
DateTime add_date;
int userTotalFileSize;
int perFileSize ;
int totalFileSize;
string[] allowedExtensions;
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)
{
if (FileUpload1.HasFile)
uploadimg();
else if (TextBox1.Value .Length > 8)
hfName.Value = Server.HtmlEncode(TextBox1.Value );
string aa = fileTypeString();
Response.Write("<script type=text/javascript>");
Response.Write("var editor=window.opener.document.getElementById('editor');");
Response.Write("editor.innerHTML+='" + aa + "';");
Response.Write("window.close();");
Response.Write("</script>");
}
}
private string fileTypeString()
{
string returnStr;
int x = hfName.Value.LastIndexOf(".");
string fileExt = hfName.Value.Substring(x + 1);
if (fileExt == "swf")
returnStr = "<object classid=clsid:D27CDB6E-AE6D-11CF-96B8-444553540000 id=obj1 codebase=http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0 border=0 width=400 height=300><param name=movie value=" + hfName.Value + "><param name=quality value=High><PARAM NAME=Play VALUE=1><PARAM NAME=Loop VALUE=1><embed src=" + hfName.Value + " pluginspage=http://www.macromedia.com/go/getflashplayer type=application/x-shockwave-flash name=obj1 width=400 height=300 quality=High></object>"+"<br/>如果沒有開始播放,是因為windows的安全設(shè)置問題,請左鍵點擊激活flash控件后,右鍵點擊flash選擇播放<br/>";
else if (fileExt == "mp3" || fileExt == "wmv" || fileExt == "wma")
returnStr = "<EMBED src="+hfName .Value +" width=270 height=60 type=audio/mpeg LOOP=true AUTOSTART=true></EMBED>";
else
returnStr = "<a href=" + hfName.Value + ">" + hfName.Value + "</a>";
return returnStr;
}
private void writeSetting()
{
firstpage f1 = new firstpage();
f1.getSetting();
this.perFileSize = f1.perFileSize;
this.totalFileSize = f1.totalFileSize;
myfunc m1 = new myfunc();
this.allowedExtensions = m1.splitStr(f1.fileExt);
//獲取用戶已經(jīng)使用的空間大小
string x = getUserTotalFileSize();
if (x != "")
this.userTotalFileSize = Convert.ToInt32(x);
else
this.userTotalFileSize = 0;
setting.InnerHtml = "允許上傳的文件類型為" + f1.fileExt;
setting.InnerHtml += "<br>允許上傳的單個文件大小為" + perFileSize+"KB";
setting.InnerHtml += "<br>您還可以使用的空間為" + (totalFileSize*1000 - userTotalFileSize)/1000+"kB";
}
private string getUserTotalFileSize()
{
string sql = "select sum(picSize) from pic where userid=" + this.userid;
openDb open1 = new openDb(sql);
return open1.getValue();;
}
private string uploadimg()
{
Boolean fileOK = false;
//檢驗是否指定后綴
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;
for (int i = 0; i < this.allowedExtensions .Length; i++)
{
if (fileExtension == this.allowedExtensions[i])
fileOK = true;
}
this.fileSize = FileUpload1.PostedFile.ContentLength;
//利用postedFile.ContentLength來判斷文件長度,檢查是否符合文件大小,250000就是250k字節(jié)
//判斷單個文件大小是否符合要求
if (fileOK && this.fileSize < perFileSize * 1000)
fileOK = true;
else
fileOK = false;
//判斷用戶總共已經(jīng)上傳了多少文件
if (fileOK && this.userTotalFileSize < totalFileSize * 1000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(this.fullFileName);
saveToDb();
string path = Request.ApplicationPath;
if (path != "/")
hfName.Value = path + "/uploadfiles/" + randFileName;
else
hfName.Value = "/uploadfiles/" + randFileName;
}
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 + ",'file.gif','" + 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>");
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -