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

推荐订阅源

Simon Willison's Weblog
Simon Willison's Weblog
P
Privacy International News Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
T
Troy Hunt's Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
O
OpenAI News
Cloudbric
Cloudbric
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Help Net Security
Help Net Security
Cyberwarzone
Cyberwarzone
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Spread Privacy
Spread Privacy
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tenable Blog
S
Security @ Cisco Blogs
N
News and Events Feed by Topic
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
宝玉的分享
宝玉的分享
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
美团技术团队
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS Blog
V
Visual Studio Blog
P
Proofpoint News Feed
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
Hugging Face - Blog
Hugging Face - Blog
腾讯CDC
L
LangChain Blog
The Register - Security
The Register - Security
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东

博客园 - 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