上传图片

public class UPLoadFileToServer
{
 public UPLoadFileToServer()
 {
  //
  //TODO: 在此处添加构造函数逻辑
  //
 }
    /// <summary>
    /// asp.net 2.0上传文件类,返回上传后的文件名及虚拟路径。
    /// </summary>
    /// <param name="inputFile"></param>
    /// <param name="strSaveDir">上传路径</param>
    /// <param name="AllowFileEx">可以上传的文件类型(数组)</param>
    /// <param name="MaxFileLength">可以上传文件的最大大小(单位为KB)</param>
    /// <returns>返回上传数组,0文件的虚拟路径(包括文件名),1文件名</returns>
    public string[] UpLoadFile(System.Web.UI.WebControls.FileUpload inputFile, string strSaveDir, string[] AllowFileEx, int MaxFileLength)
    {
        System.Web.HttpContext CT = System.Web.HttpContext.Current;
        string[] sReturn = new string[2];
        if (inputFile.PostedFile.ContentLength > 0)
        {

            string AllowFileExList = "";         //可以上传的文件类型的字符串(通过AllowFileEx得到)
            string strName = inputFile.PostedFile.FileName;
            bool AllowUpLoad = false;
            string ShowMessage = "";          //设置返回信息
            int intExt = strName.LastIndexOf(".");      //取得文件名(抱括路径)里最后一个"."的索引
            string strExt = strName.Substring(intExt);     //取得文件扩展名
            for (int j = 0; j <= AllowFileEx.Length - 1; j++)
                AllowFileExList = AllowFileExList + AllowFileEx[j];

            for (int i = 0; i <= AllowFileEx.Length - 1; i++)
            {
                if (strExt == AllowFileEx[i])
                {
                    AllowUpLoad = true;
                    ShowMessage = "";
                    break;
                }
                else
                {
                    AllowUpLoad = false;
                    ShowMessage = "-不允许上传" + strExt + "类型的文件,只有上传" + AllowFileExList + "文件";
                }
            }
            if (inputFile.PostedFile.ContentLength / 1024 > MaxFileLength)
            {
                AllowUpLoad = false;
                ShowMessage = ShowMessage + "-文件大小超出限制,最多只能上传" + MaxFileLength.ToString() + "K\\n";

            }
            if (!AllowUpLoad)
            {
                CT.Response.Write("<script>alert(\"" + ShowMessage + "\");</script>");
                sReturn[0] = "0";
            }
            else
            {
                try
                {
                    DateTime datNow = DateTime.Now;
                    string strNewName = System.DateTime.Now.Date.ToShortDateString() + "." + System.DateTime.Now.Minute.ToString() + "." + System.DateTime.Now.Second.ToString() + "." + System.DateTime.Now.Millisecond.ToString() + "_" + inputFile.PostedFile.ContentLength.ToString() + strExt;
                    string paDir = CT.Server.MapPath(strSaveDir + strNewName);
                    inputFile.PostedFile.SaveAs(paDir);
                    sReturn[0] = strSaveDir + strNewName;

                    sReturn[1] = strNewName;
                    CT.Response.Write("<script>alert(\"已成功上传文件!\");</script>");
                }
                catch
                {
                    CT.Response.Write("<script>alert(\"很抱歉,文件上传失败请与管理员联系,谢谢!\");</script>");
                }
            }
        }
        else
        {
            CT.Response.Write("<script>alert(\"请选择你要上传的文件!\");</script>");
        }
        return sReturn;
    }

}

posted @ 2009-09-28 08:53  No One  阅读(135)  评论(0编辑  收藏  举报