第一种:
通过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" οnclick="returnImage(this.form.elements['ip'].value,this.form.elements['iw'].value,this.form.elements['ih'].value);"
type="button" value=" 确定 ">