ASP.NET接收Http发送的图片

一、模拟发送端

      <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
      <title>图片上传</title>
  </head>
  <body>
      <form action="UpLoad.aspx" method="post" enctype="multipart/form-data">
          <input type="file" name="file1" />
          <input type="submit" name="ok"  value="submit" />
      </form>
  </body>
  </html>

二、接收端.cs代码

     

 1 protected void Page_Load(object sender, EventArgs e)
 2         {
 3             HttpFileCollection files = Request.Files;
 4             HttpPostedFile postedFile = files["file1"];
 5 
 6             if (postedFile.ContentLength > 3072000) Response.Write("<script>alert('文件大小不能大于300K!')</script>"); 
7 string fileName, fileExtension; 8 fileName = System.IO.Path.GetFileName(postedFile.FileName); 9 string SaveFilePath = ""; 10 if (fileName != "") 11 { 12 SaveFilePath = "/UploadFile/" + System.DateTime.Now.ToString("yyyymmddhhmmss") + ".jpg"; 13
14 fileExtension = System.IO.Path.GetExtension(fileName).ToLower(); 15 if (fileExtension != ".jpg") Response.Write("<script>alert('文件格式不正确,你只能上传jpg格式文件!')</script>");
16
17 postedFile.SaveAs(Server.MapPath(SaveFilePath)); 18 19 } 20 }

 

 

posted @ 2012-04-14 17:04  一方一净土  阅读(2736)  评论(0编辑  收藏  举报