惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Apple Machine Learning Research
Apple Machine Learning Research
月光博客
月光博客
量子位
IT之家
IT之家
Jina AI
Jina AI
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
AWS News Blog
AWS News Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
P
Proofpoint News Feed
S
Schneier on Security
Spread Privacy
Spread Privacy
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
雷峰网
雷峰网
L
LINUX DO - 热门话题
博客园 - 聂微东
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Security Latest
Security Latest
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
K
Kaspersky official blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
Last Week in AI
Last Week in AI
博客园 - Franky
G
GRAHAM CLULEY
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
Tailwind CSS Blog
L
LINUX DO - 最新话题
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
P
Privacy International News Feed
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
Schneier on Security
Schneier on Security
V
V2EX
V
Visual Studio Blog
S
Security @ Cisco Blogs
博客园 - 叶小钗
H
Hacker News: Front Page
小众软件
小众软件
WordPress大学
WordPress大学
V2EX - 技术
V2EX - 技术
美团技术团队

博客园 - aimar

常见的 Web 项目转换问题及解决方案(转载) 在VS2005 正确地创建、部署和维护由1.1迁移到ASP.NET 2.0 应用程序 ajax用户控件中读取session的问题 - aimar - 博客园 世界杯分组与赛程 可否在vs2005中开发asp.net1.1的项目? 试用了一下google的个性化主页 Asp.net WebControls基本概念 连接sql2005 SqlServer2000的xml功能 Oracle相关资源 关于使用存储过程的一些好处以及注意事项 DataGrid生成序号列 - aimar - 博客园 asp.net常用函数 Asp.Net2.0编译模式 Asp.Net2.0的改变 ASP.NET 2.0 Beta2中页面编译模型的变化 数据库访问程序块的效率问题 Rss应用(二) 抽取Rss Rss应用(一) 创建rss
DotNet2.0中上传文件 - aimar - 博客园
aimar · 2006-04-17 · via 博客园 - aimar

ASP.NET 2.0 提供新 FileUpload 服务器控件来上传文件。

 1<%@ Page Language="C#" %>
 2<script runat="server">
 3    protected void Button1_Click(object sender, EventArgs e)
 4    {
 5        if (FileUpload1.HasFile)
 6            try
 7            {
 8                FileUpload1.SaveAs("C:\\Uploads\\" + 
 9                     FileUpload1.FileName);
10                Label1.Text = "File name: " +
11                     FileUpload1.PostedFile.FileName + "<br>" +
12                     FileUpload1.PostedFile.ContentLength + " kb<br>" +
13                     "Content type: " +
14                     FileUpload1.PostedFile.ContentType;
15            }

16            catch (Exception ex)
17            {
18                Label1.Text = "ERROR: " + ex.Message.ToString();
19            }

20        else
21        {
22            Label1.Text = "You have not specified a file.";
23        }

24    }

25</script>
26
27<HTML xmlns="http://www.w3.org/1999/xHTML" >
28<head runat="server">
29    <title>Upload Files</title>
30</head>
31<body>
32    <form id="form1" runat="server">
33    <div>
34        <ASP:FileUpload ID="FileUpload1" runat="server" /><br />
35        <br />
36        <ASP:Button ID="Button1" runat="server" OnClick="Button1_Click" 
37         Text="Upload File" /> <br />
38        <br />
39        <ASP:Label ID="Label1" runat="server"></ASP:Label></div>
40    </form>
41</body>
42</HTML>
43

使用验证控件限制上载到服务器的文件类型

<ASP:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
<ASP:Button ID="Button1" runat="server" OnClick="Button1_Click" 
 Text
="Upload File" /> <br />
<br />
<ASP:Label ID="Label1" runat="server"></ASP:Label>
<ASP:RegularExpressionValidator 
 id
="RegularExpressionValidator1" runat="server" 
 ErrorMessage
="Only mp3, m3u or mpeg files are allowed!" 
 ValidationExpression
="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))
    +(.mp3|.MP3|.mpeg|.MPEG|.m3u|.M3U)$" 
 ControlToValidate="FileUpload1"></ASP:RegularExpressionValidator>
<br />
<ASP:RequiredFieldValidator 
 id
="RequiredFieldValidator1" runat="server" 
 ErrorMessage
="This is a required field!" 
 ControlToValidate
="FileUpload1"></ASP:RequiredFieldValidator>

同时上传多个文件

 1protected void Button1_Click(object sender, EventArgs e)
 2{
 3   string filepath = "C:\\Uploads";
 4   HttpFileCollection uploadedFiles = Request.Files;
 5    
 6   for (int i = 0; i < uploadedFiles.Count; i++)
 7   {    
 8      HttpPostedFile userPostedFile = uploadedFiles[i];
 9    
10      try
11      {    
12         if (userPostedFile.ContentLength > 0 )
13         {
14            Label1.Text += "File #" + (i+1+ 
15               "";
16            Label1.Text += "File Content Type: " + 
17               userPostedFile.ContentType + "";
18            Label1.Text += "File Size: " + 
19               userPostedFile.ContentLength + "kb";
20            Label1.Text += "File Name: " + 
21               userPostedFile.FileName + "";
22    
23            userPostedFile.SaveAs(filepath + "\\" + 
24               System.IO.Path.GetFileName(userPostedFile.FileName));
25    
26            Label1.Text += "Location where saved: " + 
27               filepath + "\\" + 
28               System.IO.Path.GetFileName(userPostedFile.FileName) + 
29               "
30";
31         }
    
32      }
 
33      catch (Exception Ex)
34      {    
35         Label1.Text += "Error: " + Ex.Message;    
36      }
    
37   }
    
38}

39
40