首页 > 代码库 > 在ASP.NET中实现图片、视频文件上传方式

在ASP.NET中实现图片、视频文件上传方式

一、图片

1、在前端用<asp:FileUpload ID="UpImgName" runat="server"/>控件

2、在后台.cs中写上

  protected void btnSubmit_Click(object sender,EventArgs e)

{

  string strImgPath=string.Empty;

  string strDateTime=dateTime.Now.Tostring("yyyyMMddhhmmss");

  strImgPath=this.UpImgPath.PostedFile.FileName;

  if(strImgPath!="")

  {

    string extension="";//扩展名

    extension=Path.GetExtension(strImgPath).ToLower();

    if(extension==".jpg"||extension==".jpeg"||extension==".bmp||extension==""gif")

    {

      if(this.UpImgPath.PostedFile.ContentLength>1000000)//图片大小是否大于1M

      {

        Response.Write("<script>alert(‘图片太大‘)</script>");  

        return;    

      }

      strImgPath="/Images/"+strDateTime+extension;

      string UpPath=Server.MapPath(strImgPath);  

      this.UpImgPath.PostedFile.SaveAs(UpPath);    

    }

    else

    {

      Response.Write("<script>alert(‘图片格式错误‘)</script>");

      return ;

    }  

  }

}

二、视频,文件

1、在前端用<asp:FileUpload ID="UpVideoName" runat="server"/>控件

2、在后台.cs中写上

  protected void btnSubmit_Click(object sender,EventArgs e)

{

  string strVideoPath=string.Empty;

  string strDateTime=DateTime.Now.Tostring("yyyyMMddhhmmss");

  strVideoPath=this.UpVideoName.PostedFile.FileName;

  if(strVideoPath!="")

  {

    string extension="";

    extension=Path.GetExtension(strVideoPath).ToLower();

    if(extension==".flv" || extension == ".doc" || extension == ".docx" || extension == ".zip" || extension == ".rar")

    {

      if(this.UpVideoName.PostedFile.ContentLength>30000000)

      {

        Response.Write("<script>alert(‘视频或文件太大‘)</script>");

        return;

      }

      strVideoPath="/VideoOrFile/"+strDateTime+extension;

      string UpPath=Server.MapPath(strVideoPath);

      this.UpVideoName.PostedFile.SaveAs(UpPath);

    }

    else

    {

      Response.Write("<script>alert(‘存储的格式不正确‘)</script>")

      return;

    }

  }

}