第一種:
通過2005裡面帶的UPLOAD工具來上傳:
public static String UploadImage(System.Web.UI.WebControls.FileUpload FileBox, string SavePath)
{
DateTime datTime = System.DateTime.Now;
string FileName = FileBox.FileName; //檔案名稱
string Extension = ""; //副檔名
string strTemp = "";
string relativePath = ""; //相對路徑
string FilePath = ""; //實體路徑
if (FileBox.HasFile)
{
Extension = Path.GetExtension(FileName);
strTemp = datTime.ToShortDateString().Replace("-", "");
FileName = datTime.ToLongTimeString().Replace(":", "") + datTime.Millisecond + (new Random()).Next(100000)+Extension;
FileName = strTemp + FileName;
int FileSize = FileBox.PostedFile.ContentLength;
if (FileSize > 50000)
{
System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('檔案超過50K,無法上傳!')</script>");
return relativePath;
}
relativePath = "~/UploadFiles/" + SavePath + "/"+strTemp+"/";
FilePath = System.Web.HttpContext.Current.Server.MapPath(relativePath);
CreateDirectory(FilePath);
FilePath += FileName;
relativePath = "/UploadFiles/" + SavePath + "/" + strTemp + "/";
relativePath += FileName;
FileBox.SaveAs(FilePath);
//System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('上傳成功')</script>");
return relativePath;
}
else
{
//System.Web.HttpContext.Current.Response.Write("<script language=javascript>window.alert('上傳失敗')</script>");
return relativePath;
}
}
這個是另外一種方法:
此方法用於在FREETEXTBOX中的上傳圖片:(參照上傳圖片)
if (UploadFile.PostedFile.FileName.Trim() != "")
{
if (IsValidFileType(UploadFile.PostedFile.FileName))
{
try
{
string UploadFileName = "";
string UploadFileDestination = "";
string filename= "";
int ppp = 0;
filename=(DateTime.Now.ToString()).Replace(":","");
filename= filename.Replace(" ","");
filename= filename.Replace("-","");
UploadFileName = UploadFile.PostedFile.FileName;
ppp=UploadFileName.LastIndexOf(".");
UploadFileName=UploadFileName.Substring(ppp,UploadFileName.Length-ppp);
filename = filename+UploadFileName;
UploadFileDestination = HttpContext.Current.Request.PhysicalApplicationPath;
UploadFileDestination += CurrentImagesFolder.Value;
UploadFileDestination += "//";
UploadFile.PostedFile.SaveAs(UploadFileDestination + filename);
string urrl = Server.MapPath("../images/") + filename;
double width=0;
double height=0;
System.Drawing.Bitmap imgObj = new Bitmap(urrl);
width=imgObj.Width;
height=imgObj.Height;
double k;//金儒 2005.5.11
if ((width != 0) && (height != 0))
{
k=Convert.ToDouble( width/height);
if(k>=1)
{
if (width>=750)
{
width=750;
height=width/k;
}
}
else
{
if (height>=750)
{
height=750;
width=k*height;
}
}
}
string imgurl = @"../AspNetForums/images/" + filename;
this.iw.Value = width.ToString();
this.ih.Value = height.ToString();
this.ip.Value = imgurl;
ResultsMessage.Text = UploadSuccessMessage;
}
catch
{
ResultsMessage.Text = UploadFailureMessage;
}
}
else
{
ResultsMessage.Text = InvalidFileTypeMessage;
}
}
其中的width ,height 用於傳回值,用來在編輯框裡顯示大小。
function returnImage(imagename,width,height) { var imgAr = new Array(); imgAr["filename"] = imagename; imgAr["width"] = width; imgAr["height"] = height; window.parent.returnValue = imgAr; window.parent.close(); }
(首先width ,height分別付給this.form.elements['ip'].value,this.form.elements['iw'].value,this.form.elements['ih'].value 然後“確定”傳到接收的頁面)
<INPUT id="enter" onclick="returnImage(this.form.elements['ip'].value,this.form.elements['iw'].value,this.form.elements['ih'].value);"
type="button" value=" 確定 ">