zoukankan      html  css  js  c++  java
  • asp.net type=file前后台合作 在上传图片到服务器

    前台

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
    </head>
    <body>
        <form runat="server" id="form1" method="post" enctype="multipart/form-data">
            <input name="f" type="file" />
            <input name="s" type="submit" />
        </form>
    </body>
    </html>

    后台

     System.Web.HttpFileCollection _file = System.Web.HttpContext.Current.Request.Files;
            if (_file.Count > 0)
            {
                //文件大小
                long size = _file[0].ContentLength;
                //文件类型
                string type = _file[0].ContentType;
                //文件名
                string name = _file[0].FileName;
                //文件格式
                string _tp = System.IO.Path.GetExtension(name);
    
                if (_tp.ToLower() == ".jpg" || _tp.ToLower() == ".jpeg" || _tp.ToLower() == ".gif" || _tp.ToLower() == ".png" || _tp.ToLower() == ".swf")
                {
                    //获取文件流
                    System.IO.Stream stream = _file[0].InputStream;
                    //保存文件
                    string saveName = DateTime.Now.ToString("yyyyMMddHHmmss") + _tp;
                    string path = DataFactory.WFile.FileUploadPath + "/upload/area/" + saveName;
                   // string path = Server.MapPath("images/"+savename);自己编写
                    _file[0].SaveAs(path);
                }
            }

    总结:

    1.form 必须有runat="server"标记,

    2.form  必须有enctype="multipart/form-data"标记,

    3.<input type="file" />的runat="server"标记可选

    完!

  • 相关阅读:
    11.变分推断
    10.高斯混合模型GMM
    9.EM 算法
    8.指数族分布
    7.概率图模型(表示/推断/学习)
    6.核方法
    二分查找
    2.3 数据结构---数组(连续)
    C#开发Windows服务的基础代码
    C#与C++之间类型的对应{转}
  • 原文地址:https://www.cnblogs.com/wwz-wwz/p/6244725.html
Copyright © 2011-2022 走看看