zoukankan      html  css  js  c++  java
  • ASP.NET FileUpload控件使用

     1  protected void fileBut_Fun(object sender, EventArgs e)
     2     {
     3 
     4          
     5 
     6          if (FileUpload1.HasFile)//判读是否有文件
     7          {
     8              
     9              String filename = FileUpload1.FileName;//得到文件在本地的路径,及全路径
    10              String kzm = filename.Substring(filename.LastIndexOf("."));//将扩展名存放到变量kzm中
    11              String uploadfilename = Server.MapPath("upload") + "\" + filename;//得到文件在服务器上的路径和文件名。
    12            
    13              if (kzm.Equals(".bat"))//判断扩展名
    14              {
    15                  Response.Write("<script>alert('不允许上传.bat文件');</script>");
    16                  return;
    17              }
    18              if (File.Exists(uploadfilename))
    19              {         //判断重名
    20                  Response.Write("<script>alert('命名重复,请更换图片名称!');</script>");
    21              }
    22              else
    23              {
    24                  int a = FileUpload1.PostedFile.ContentLength;
    25                  if (a <= 8388608)
    26                  {
    27                      try
    28                      {
    29                          int n = FileUpload1.PostedFile.ContentLength;
    30 
    31                          Response.Write(Convert.ToString(n));
    32                          FileUpload1.SaveAs(uploadfilename);//将文件上传到服务器上。
    33 
    34                      }
    35                      catch (Exception ex)
    36                      {
    37                          Response.Write("<script>alert('" + ex.Message.ToString() + "');</script>");
    38                      }
    39                  }
    40                  else 
    41                  {
    42                      Response.Write("<script>alert('附件不得超过8M!');</script>");
    43 
    44                  }
    45 
    46 
    47              }
    48          }
    49          else
    50          {
    51              Response.Write("<script>alert('你还没选择上传文件!');</script>");
    52 
    53          }
    54         
    55         }

    WEB.XML文件中控制上传大小 

     <httpRuntime executionTimeout="500" maxRequestLength="409600" useFullyQualifiedRedirectUrl="false" minFreeThreads="8" minLocalRequestFreeThreads="4" appRequestQueueLimit="100" />
    maxRequestLength:设置上传大小。此例400MB.
    FileUpload1.PostedFile.ContentLength;获取文件大小 KB字节为单位。
     
  • 相关阅读:
    使用SoapUI发送Post请求
    webservice wsdl地址
    BurpSuite(二) proxy 模块
    BurpSuite(一)工具介绍
    Windows下配置DVWA
    .Net WebApi 支持跨域访问使用 Microsoft.AspNet.WebApi.Cors
    小程序学习 (一) 入门准备
    学习EChart.js(四) 移动端显示
    JQuery 获取select 的value值和文本值
    JS 获取URL参数
  • 原文地址:https://www.cnblogs.com/eason-chan/p/3638416.html
Copyright © 2011-2022 走看看