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

推荐订阅源

I
InfoQ
C
CERT Recently Published Vulnerability Notes
The Last Watchdog
The Last Watchdog
P
Proofpoint News Feed
D
Darknet – Hacking Tools, Hacker News & Cyber Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
GbyAI
GbyAI
T
Tenable Blog
博客园 - 三生石上(FineUI控件)
P
Privacy & Cybersecurity Law Blog
Simon Willison's Weblog
Simon Willison's Weblog
Jina AI
Jina AI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
博客园_首页
F
Fortinet All Blogs
博客园 - Franky
Latest news
Latest news
Last Week in AI
Last Week in AI
T
Threat Research - Cisco Blogs
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
U
Unit 42
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Hugging Face - Blog
Hugging Face - Blog
D
Docker
Project Zero
Project Zero
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
F
Full Disclosure
D
DataBreaches.Net
Google DeepMind News
Google DeepMind News
Cisco Talos Blog
Cisco Talos Blog
Y
Y Combinator Blog
WordPress大学
WordPress大学
C
Cyber Attacks, Cyber Crime and Cyber Security
H
Help Net Security
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
S
Schneier on Security
C
Cybersecurity and Infrastructure Security Agency CISA
P
Proofpoint News Feed
PCI Perspectives
PCI Perspectives
Cloudbric
Cloudbric
V
Visual Studio Blog
Recorded Future
Recorded Future
人人都是产品经理
人人都是产品经理

博客园 - 落尘

.NET上传图片加文字和水印图片源码 - 落尘 - 博客园 ASP.NET下的多文件上传 ASP.NET 2.0 多文件上传小经验 在ASP.NET程序中实现语音合成 .Net平台下开发中文语音应用程序 语音合成与识别技术在C#中的应用 如何防止ASP 木马在服务器上运行 用ASP实现在线压缩与解压缩 asp对象化之:基于adodb.stream的文件操作类 js鼠标及对象坐标控制属性 NET中各种数据库连接大全 几个ASP.NET技巧 设计ASP.NET应用程序的七大绝招 ASP.NET中如何调用存储过程 Asp.net cache 简述 如何用C#把Doc文档转换成rtf格式 单点登陆 硬盘双击无法打开是咋回事 Asp.net直接保存文件到客户端
文件的上传和下载
落尘 · 2007-02-05 · via 博客园 - 落尘

在Web编程中,我们常需要把一些本地文件上传到Web服务器上,上传后,用户可以通过浏览器方便地浏览这些文件,应用十分广泛。

那么使用C#如何实现文件上传的功能呢?

首先,在你的Visual C# web project 中增加一个上传用的Web Form,为了要上传文件,需要在ToolBox中选择HTML类的File Field控件,将此控件加入到Web Form中,然而此时该控件还不是服务端控件,我们需要为它加上如下一段代码:<input id=uploadfile1 type=file size=49 runat="server">,这样它就成为服务端控件了,如果需要同时上传数个文件时,我们可以相应增加此控件。

需要注意的是代码中一定要把<form>的属性设置成为:

<form method=post encType=multipart/ form-data runat="server">

如果没有这个属性,就不能实现上传。

然后在此Web Form中增加一个Web Form类的Button,双击Button添加如下代码:

//上传图片的程序段
DateTime now = DateTime.Now ;
//取现在时间到DataTime类的对象now中
string strBaseLocation = "D:\\web\\FC\\pic\\";
//这是文件将上传到的服务器的绝对目录
if (uploadfile1.PostedFile.ContentLength != 0) //判断选取对话框选取的文件长度是否为0
  {uploadfile1.PostedFile.SaveAs(strBaseLocation+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg");
//执行上传,并自动根据日期和文件大小不同为文件命名,确保不重复
Label1.Text="图片1已经上传,文件名为:"+now.DayOfYear.ToString()+uploadfile1.PostedFile.ContentLength.ToString()+".jpg";


【注意事项】
1. 上传文件不可以无限大;

2. 要注意IIS的安全性方面的配合;

3. 用Visual Studio 的安装项目做安装程序的时候,请注意安装程序所在的绝对路径问题;

4. 注意文件上传后的重名问题。
 

下载功能如下:

filename:实际文件名 
oldName:上传的文件名 
                       public  void  DownFile(string  filename,string  OldName) 
                       { 
                                   System.IO.FileInfo  file  =  new  System.IO.FileInfo(filename); 
 
                                   Response.Clear(); 
                                   Response.AddHeader("Content-Disposition",  "attachment;  filename="  +  OldName); 
                                   Response.AddHeader("Content-Length",  file.Length.ToString()); 
                                   Response.ContentType  =  "application/octet-stream"; 
                                   Response.WriteFile(file.FullName); 
                                   Response.End(); 
                       }