zoukankan      html  css  js  c++  java
  • Asp.net 文件操作

    1.读取项目文件夹里的图片切换背景图

    第一步:配置文件web.config里添加

    <system.web>
    <connectionStrings>
    
    <!--name 是自定义的,connectionString 的值是你存放图片的虚拟路径-->
    <add name="strCon" connectionString="~/Image"/>
    </connectionStrings>
    </system.web>

    第二步:aspx页面

    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
    body {
    background-image:url("<%=Url %>")
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div >
    </div>
    </form>
    </body>
    </html>

    第三步:aspx.cs代码

    public partial class Background : System.Web.UI.Page
    {
    protected string Url = "";
    protected void Page_Load(object sender, EventArgs e)
    {
    Url = Test();
    }
    //获取图片路径
    public string Test()
    {
    //配置文件里配置虚拟路径引用Configuration
    string filepath =this.Server.MapPath(ConfigurationManager.ConnectionStrings["strCon"].ConnectionString);
    
    //引用IO
    string[] files= Directory.GetFiles(filepath);
    
    if (files.Length > 0)
    {
    
    //随机读取
    Random rand = new Random();
    int num= rand.Next(files.Count());
    FileInfo file = new FileInfo(files[num]);
    if (file.Extension == ".jpg" || file.Extension == "gif")
    {
    return "/Image/" + file.Name;
    }
    
    }
    return "/images/1.jpg";
    
    }
    }
    View Code

    2.将文件上传到服务器里的文件夹里

    第一步:在服务器网站下建立一个虚拟目录  虚拟目录指向你要存放文件的路径 eg:  c:uploadfile

    第二步:配置webconfig

    <system.web>
    <appSettings> 
      <!--图片存放路径-->
        <add key="SaveImageUrl" value="C:uploadfile" />
      <!--虚拟目录-->
        <add key="ImageUrlprefix" value="http://198.16.10.12:8015/UploadFile/" />
    </appSettings>
    </system.web>

    第三步上传文件页面

    //获取配置的路径 
    public static string  prefix = System.Configuration.ConfigurationManager.AppSettings["ImageUrlprefix"].ToString();
    public static string picpath = System.Configuration.ConfigurationManager.AppSettings["SaveImageUrl"].ToString();
    
    
    //文件存到服务器的文件夹里
    文件控件.SaveAs( picpath  + 文件名字);
    存到数据库的文件路径= prefix + fileName3;
  • 相关阅读:
    java swing学习
    JCheckBox相关知识点
    【python 第五日】 函数闭包与装饰器
    【python第四日】 文件处理 生成器 迭代器
    【Python3 第三日】%和format格式化输出 函数
    【python第二日】运算符 数据类型(数字 字符串 列表 元组 字典 集合) 重新定义比较大小
    怎么设置博客园样式
    【python】第一日 python2和python3区别 命名方式 三种结构
    mybatis-generator.xml
    SpringBoot集成mybatis和mybatis generator
  • 原文地址:https://www.cnblogs.com/youchim/p/3633669.html
Copyright © 2011-2022 走看看