zoukankan      html  css  js  c++  java
  • 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]。

  • 相关阅读:
    部署 AppGlobalResources 到 SharePoint 2010
    还原一个已删除的网站集
    使用仪表板设计器配置级联筛选器 (SharePoint Server 2010 SP1)
    File or arguments not valid for site template
    Pex and Moles Documentation
    Content Query Webpart匿名访问
    Running Moles using NUnit Console from Visual Studio
    Calling a WCF Service using jQuery in SharePoint the correct way
    Updating Content Types and Site Columns That Were Deployed as a Feature
    asp.net中判断传过来的字符串不为空的代码
  • 原文地址:https://www.cnblogs.com/lampon/p/3419529.html
Copyright © 2011-2022 走看看