图片上传的方法!

第一种:

通过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=" 确定 ">


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值