码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-07-18 21:05:24      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:os   文件   io   re   c   ar   

一、图片

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;

    }

  }

}

在ASP.NET中实现图片、视频文件上传方式,布布扣,bubuko.com

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

标签:os   文件   io   re   c   ar   

原文地址:http://www.cnblogs.com/LIANYUGOOD/p/3850226.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!