zoukankan      html  css  js  c++  java
  • HttpPostedFile类——多文件上传

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title></title>
    </head>
    <body>
    <img src="waterImg.ashx?imgName=1.jpg" />
    <img src="1.jpg" />
    <a href="GetFileDownload.ashx?fn=googlelogo.jpg">点击下载!</a>hellowrold!123456789988
    <form action="FileUp.ashx" method="post" enctype="multipart/form-data">
        <input type="file" name="fileU" />
        <input type="file" name="fileU2" />
        <input type="submit" value="上传" />
        <input type="hidden" name="isPostBack" value="1"/>
    </form>
    </body>
    </html>

    上为HTML代码:

    下为ashx代码:

     1 <%@ WebHandler Language="C#" Class="FileUp" %>
     2 
     3 using System;
     4 using System.Web;
     5 
     6 public class FileUp : IHttpHandler {
     7 
     8     string strHtml = "<html><head></head></html>";
     9     string modelPath = "model.html";
    10     
    11     public void ProcessRequest (HttpContext context) {
    12         System.Web.UI.WebControls.Button btn = new System.Web.UI.WebControls.Button();
    13         
    14         //尝试检查浏览器是否传递过来一个叫 isPostBack 的控件的值
    15         string isPostback = context.Request.Form["isPostBack"];
    16         
    17         if (!string.IsNullOrEmpty(isPostback))//上传提交
    18         {
    19             //context.Request.Files -- 这里面有上传上来的文件数据(客户端用户选择了几个文件,那么这个Files里就有几个元素)
    20             if (context.Request.Files.Count > 0)
    21             {
    22                 for (int i = 0; i < context.Request.Files.Count; i++)
    23                 {
    24                     //获得当前循环到的文件对象
    25                     HttpPostedFile hPostedFile = context.Request.Files[i];
    26                     if (hPostedFile.FileName != "")
    27                     {
    28                         //C:Documents and SettingsAdministrator.ICBCOA-6E96E6BE桌面1.jpg
    29                         string filePath = hPostedFile.FileName;
    30                         string fileName = System.IO.Path.GetFileName(filePath);//1.jpg
    31                         //G:WebTwoHandlerupload1.jpg
    32                         string fileServerPath = context.Server.MapPath("upload\" + fileName);
    33                         hPostedFile.SaveAs(fileServerPath);//保存在服务器指定的路径上
    34                         context.Response.Write("上传成功!:)" + hPostedFile.FileName);
    35                     }
    36                 }
    37             }
    38         }
    39         else//如果是通过url访问,则读取HTML模板并输出给浏览器(用户就可以看见文件选择框和按钮了)
    40         {
    41             modelPath = context.Server.MapPath(modelPath);//获得模板服务器物理路径
    42             strHtml = System.IO.File.ReadAllText(modelPath);//读取HTML代码
    43             context.Response.Write(strHtml); 
    44         }
    45     }
    46  
    47     public bool IsReusable {
    48         get {
    49             return false;
    50         }
    51     }
    52 
    53 }
    三思而又行。
  • 相关阅读:
    Java——读取和写入txt文件
    Java——去除字符串中的中文
    web.xml——Error:cvc-complex-type.2.4.a: Invalid content was found starting with element
    poi--读取不同类型的excel表格
    poi——读取excel数据
    java静态代码块
    Java-main方法中调用非static方法
    dom4j--解析xml文件
    python发送邮件
    JWT认证原理及使用
  • 原文地址:https://www.cnblogs.com/jun-jie/p/3255575.html
Copyright © 2011-2022 走看看