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

    来源于:http://www.cnblogs.com/SkySoot/p/3525139.html

    html 表单上传文件

           一般处理程序由于没有 apsx 页面的整个模型和控件的创建周期,而比较有效率。这里写一个用 html 表单进行文件上传的示例。

           1. 表单元素选用 <input type="file"> 控件。

           2. form 表单需要设置 enctype="multipart/form-data" 属性,请求报文体中数据格式也由键值对更改为数据头和数具体,并有随机边界符分割。

           3. 服务器端接收文件使用 Request.Files 属性。

           4. 使用 HttpPostedFile 的 SaveAs 方法保存文件(需转换成网站物理路径)。

    <body>
        <form action="UploadFile.ashx" method="post" enctype="multipart/form-data">
        <input type="file" name="fileUpload" />
        <input type="submit" value="上传文件" />
        </form>
    </body>


    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/html";
        HttpServerUtility server = context.Server;
        HttpRequest request = context.Request;
        HttpResponse response = context.Response;
     
        HttpPostedFile file = context.Request.Files[0];
        if (file.ContentLength > 0)
        {
            string extName = Path.GetExtension(file.FileName);
            string fileName = Guid.NewGuid().ToString();
            string fullName = fileName + extName;
     
            string imageFilter = ".jpg|.png|.gif|.ico";// 随便模拟几个图片类型
            if (imageFilter.Contains(extName.ToLower()))
            {
                string phyFilePath = server.MapPath("~/Upload/Image/") + fullName;
                file.SaveAs(phyFilePath);
                response.Write("上传成功!文件名:" + fullName + "<br />");
                response.Write(string.Format("<img src='Upload/Image/{0}'/>", fullName));
            }
        }
    }
  • 相关阅读:
    HDU 2112 HDU Today
    HDU 1869 六度分离
    HDU 3790 最短路径问题
    HDU2066 一个人的旅行
    HDU1596 find the safest road(最短路)
    HDU 1254 推箱子(双重bfs)
    HDU 1429 胜利大逃亡(续) (bfs+状态压缩)
    HDU 1045 Fire Net
    数据结构之单链表头插法,尾插法
    Java--会移动、反弹的球
  • 原文地址:https://www.cnblogs.com/ys-wuhan/p/5834268.html
Copyright © 2011-2022 走看看