📄 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的安全设置问题,请左键点击激活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);
//获取用户已经使用的空间大小
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字节
//判断单个文件大小是否符合要求
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();
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>");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -