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 Settings\Administrator.ICBCOA-6E96E6BE\桌面\1.jpg
29                         string filePath = hPostedFile.FileName;
30                         string fileName = System.IO.Path.GetFileName(filePath);//1.jpg
31                         //G:\WebTwoHandler\upload\1.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 }

 

 

转载于:https://www.cnblogs.com/jun-jie/p/3255575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值