• html input type=file文件上传操作


    如果file框没有加runat="server",则form里一定要加上 enctype="multipart/form-data"这样才可以实现上传文件到服务器;使用了server和没有使用 runat="server"是有区别的.使用了runat="server"的form编译后,action必定是指向本身的网页。而没 有加runat="server"的form可以指向一个网页。

     <form name="form1" method="post" enctype="multipart/form-data" action="requestfile/asprece.aspx">
            <input type="file" name="file1" style="160px;" />
            <input type="submit" name="Submit" value="添加" />
     </form>

     服务端代码:

     private string retvalue = "ok";
            protected void Page_Load(object sender, EventArgs e)
            {
                if(!IsPostBack)
                {
                    HttpPostedFile req = Request.Files["file1"];
                    if (req == null || req.ContentLength < 0)
                    {
    
                        Response.Write("没有文件");
                        Response.End();
                    }
                    else
                    {
                        try
                        {
    
                           string extion = System.IO.Path.GetExtension(req.FileName.ToString());
                            string date = DateTime.Now.ToString("yyyyMMddhhmmss").ToString();
                            string src = date + extion;
                            string pathnew = Server.MapPath("~/testfile/");
                            req.SaveAs(pathnew+src);        //自带的方式保存文件
    
                            /*读取文件流保存
    
                             Stream stream = req.InputStream;
                            //string src = "test.xls";
                            string fullpathnew = pathnew + src;
    
                            if (!Directory.Exists(pathnew))
                            {
                                Directory.CreateDirectory(pathnew);
                            }
    
                            BinaryReader br = new BinaryReader(stream);
                            byte[] fileByte = br.ReadBytes((int)stream.Length);
                            // string content = fileByte.ToString();
                            using (FileStream fileStream = new FileStream(fullpathnew, FileMode.Create))
                            {
                                fileStream.Write(fileByte, 0, fileByte.Length);
                            }*/
    
                        }
                        catch (Exception es)
                        {
                            retvalue = es.Message.ToString();
    
                        }
                        finally 
                        {
                            Response.Write(retvalue);
                        }
                    }
                }

    用 Request.Files。

    • Request.Files.Count 客户端传了多少文件过来。此时不论我们使用的是服务端控件还是 HTML 控件,因为到了客户端都是一样的 <input type="file";另外,即使文件上传框中没有选择文件,都可能会当作上传了无内容的文件(视客户端浏览器)。
    • Request.Files[i].ContentLength 获取上传文件的大小,以字节为单位。
    • Request.Files[i].ContentType 获取客户端发送的文件的 MIME 内容类型。
    • Request.Files[i].FileName
    • Request.Files[i].InputStream
    • Request.Files[i].SaveAs(string filename)

    上面的 Request.Files[i],也可以是 Request.Files[name]。

  • 相关阅读:
    二级菜单实现
    Python监控日志中经常访问的ip
    Python判断一个数是否为小数
    Python-读写文件
    python-字典
    Python实现屏蔽敏感词
    Python生成随机密码
    Python-列表和元组
    Jmeter命令行参数
    启动Jmeter时遇到的几种错误
  • 原文地址:https://www.cnblogs.com/lampon/p/3419529.html
Copyright © 2020-2023  润新知