?? 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 lalablog;
public partial class fileupload : System.Web.UI.Page
{
string randFileName;
string fullFileName;
protected void Page_Load(object sender, EventArgs e)
{
String path = Server.MapPath("~/Uploadfiles/");
randomTimeString rts = new randomTimeString();
this.randFileName = rts.str+".jpg";
this.fullFileName = path + randFileName;
if (IsPostBack && FileUpload1.HasFile) uploadimg();
}
private string uploadimg()
{
Boolean fileOK = false;
//檢驗是否指定后綴
if (FileUpload1.HasFile)
{
string fileExtension = System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
fileOK = true;
}
//利用postedFile.ContentLength來判斷文件長度,檢查是否符合文件大小,250000就是250k字節
if (fileOK && FileUpload1.PostedFile.ContentLength < 250000)
fileOK = true;
else
fileOK = false;
}
if (fileOK)
{
try
{
FileUpload1.PostedFile.SaveAs(this.fullFileName );
System.Drawing.Image upimg = System.Drawing.Image.FromFile(this.fullFileName);
hfWidth .Value =upimg.Width.ToString ();
if (Convert.ToInt32(hfWidth.Value) > 400)
hfWidth.Value = "400";
hfName.Value = "uploadfiles/" + randFileName;
Label2 .Text ="<img src='"+hfName .Value +"' width=120 height=90'/>";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
}
else
{
Label1.Text = "無法上傳的文件類型或文件大小超過250K";
}
return "";
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -