现在的位置: 首页 > 综合 > 正文

FileUpload上传组件在ASP.net中最简单的运用示例

2012年12月13日 ⁄ 综合 ⁄ 共 3662字 ⁄ 字号 评论关闭

  //*******************************************************************************************
        string Savepath = Server.MapPath("~/UploadFile/");
        string temp = this.FileUpload.FileName;
        bool fileok = false;
        string newfilename = string.Empty;
        if (this.FileUpload.HasFile)
        {
            string fileException = System.IO.Path.GetExtension(this.FileUpload.FileName).ToLower();

            string[] allowedException ={ ".gif", ".bmp", ".png", ".jpg" };
            for (int i = 0; i < allowedException.Length; i++)
            {
                if (fileException == allowedException[i])
                    fileok = true;
            }
        }
        if (fileok)//图片才上传
        {
            try
            {
                newfilename = TourNewsHelper.GetNewFileName(this.FileUpload.FileName);
                if (File.Exists(Savepath + newfilename) != false)
                {
                    this.FileUpload.SaveAs(Savepath + newfilename);
                }
            }
            catch (Exception ex)
            {
                CommonHelper.DoUpAlert(this.Page, "图片上传失败!");
            }
        }
        else
        {
            CommonHelper.DoUpAlert(this.Page, "只允许上传gif、bmp、png、jpg图片");
        }

public static string GetNewFileName(string FileName)
{
 //跟据文件名产生一个由时间+随机数组成的一个新的文件名
string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
+ DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
+ DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
+ DateTime.Now.Millisecond.ToString()
//+ rand.Next(1000).ToString()
+ FileName.Substring(FileName.LastIndexOf("."), FileName.Length - FileName.LastIndexOf("."));
return newfilename;
}

//****************************************************************************************
            获得文件类型的方法还有:
            string fileContentType = FileUpload1.PostedFile.ContentType;
            if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")

*****************************************************************************
 private void UpFile()
    {
        if ((UpLoadFile.PostedFile.ContentLength > 0))
        {   ///首先取到上载文件的原始名称
            String fileName =
                UpLoadFile.PostedFile.FileName.Substring(UpLoadFile.PostedFile.FileName.LastIndexOf("\\"),
                UpLoadFile.PostedFile.FileName.Length - UpLoadFile.PostedFile.FileName.LastIndexOf("\\"));
            ///取到当前时间的年、月、日、分、秒和毫秒的值,并使用字符串格式把它们组合成一个字符串
            String fileTime = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString()
                + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString()
                + DateTime.Now.Second.ToString() + DateTime.Now.Minute.ToString()
                + DateTime.Now.Millisecond.ToString();
            ///在时间字符串后面添加一个随机数和文件的后缀名
            fileName = "\\" + fileTime + GetRandomint()
                + fileName.Substring(fileName.IndexOf("."), fileName.Length - fileName.IndexOf("."));
            if (File.Exists(Server.MapPath(Request.ApplicationPath) + "file://upload/" + fileName) == false)
            {
                ///上载文件到服务器硬盘
                UpLoadFile.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "file://upload/"
                    + fileName);
            }
            else
            {
                Response.Write("<script>alert(\"此文件已经存在,请重新命名你的文件!\")</script>");
            }
        }
        else
        {
            Response.Write("<script>alert(\"文件名和内容不能为空!\")</script>");
        }
    }

    private String GetRandomint()
    {
        Random random = new Random();
        return (random.Next(10000).ToString());    //产生一个小于10000的随机正整数
    }
----------------------------------------------------------------------------------------------------
    删除上传的文件:File.Delete(Server.MapPath(Request.ApplicationPath) + @"file://upload/200583016538151246.gif");

抱歉!评论已关闭.