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

推荐订阅源

Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
J
Java Code Geeks
博客园 - 聂微东
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
腾讯CDC
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
美团技术团队
博客园 - Franky
Google DeepMind News
Google DeepMind News
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
The Cloudflare Blog

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