zoukankan      html  css  js  c++  java
  • 文件上传

    文件上传:

    1、防止文件重名:

    string path = "Upload/" + DateTime.Now.ToString("yyyyMMddssmmssms") + Session["UserName"] + FileUpload1.FileName;
    //防止文件重名,

    2、设置文件的绝对路径:

    string endpath = Server.MapPath(path);

    3、上传:

    FileUpload1.SaveAs(endpath);

     4、限制上传的文件格式:

    string[] ns = FileUpload1.FileName.Split('.');
    //以'.'截取字符串;
    if(ns[ns.Length-1]=='.aspx'||ns[ns.Length-1]=='.dll')
    {
        Resposen.Write("<script>alert('不允许上传的文件类型!!')</ script>");
        return;
    }
    //判断最后一个'.'截取的字符串是否是代码文件等文件类型,如果是就return住;

    5、限制最好写在后台,如果写在JS,容易被破解;

    6、文件上传的大小,扩容:

    //文件上传的大小默认4096单位是KB
    //在web.config的system.web标签下:
    <httpRuntime maxRequsetLength="4096" />
    //可以输任意大小,最大不能超过int32的大小,单位KB

    7、限制可以看到的文件类型(防君子,不防小人):

    //在FileUpload控价下添加accept属性
    <asp:FileUpload ID="FileUpload1" accept=".txt,.png,.jpg,等等..." runat="server" />

    8、验证文件大小:

    在C#后台可以验证,但是文件依然会上传,超出文件大小依然报错,这样就需要用JS验证
    C#:
    int a = FileUpload1.PostFide.ContentLength; 
    //这个获取的是上传的文件的大小,默认单位b;
    JS:
    document.getElementById('Botton1').click=function(){
        if(document.getElementBtId('FileUpload1').Files[0].size>1024*1024*5)
        {
            alert('上传的文件过大');
            return false;
        }
    
    }
  • 相关阅读:
    hdu 6188 Duizi and Shunzi
    区间第k大
    AtCoder Regular Contest 081 E
    hdu 6170 Two strings
    hdu 6156 Palindrome Function
    2017百度之星初赛(B)-1006-小小粉丝度度熊 hdu 6119
    AtCoder Regular Contest 080 E
    hdu 6069 Counting Divisors
    hdu 6058 Kanade's sum (多校3)
    苹果曼和树
  • 原文地址:https://www.cnblogs.com/xinchenhui/p/8284275.html
Copyright © 2011-2022 走看看