C#实现文件上传与下载功能实例
作者:pan_junbiao 时间:2022-11-18 07:59:03
最近学习了 C#实现文件上传与下载,现在分享给大家。
1、C#文件上传
创建MyUpload.htm页面,用于测试
<form name="form1" method="post" action="UploadFile.aspx" id="form1"
enctype="multipart/form-data">
<input type="file" id="txtFile" name="picfile" /></br>
<input type="submit" value="上传" />
</form>
创建UploadFile.aspx文件,在UploadFile.aspx.cs键入如下代码:
Random rnd = new Random(); //产生随机数
private string _directory = @"/File/UploadFile"; //目录
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (RequestFilesCount > 0)
{
//判断文件大小
int length = RequestFiles[0]ContentLength;
if (length > 1048576)
{
ResponseWrite("文件大于1M,不能上传");
return;
}
string type = RequestFiles[0]ContentType;
string fileExt = PathGetExtension(RequestFiles[0]FileName)ToLower();
//只能上传图片,过滤不可上传的文件类型
string fileFilt = "gif|jpg|php|jsp|jpeg|png|";
if (fileFiltIndexOf(fileExt) <= -1)
{
ResponseWrite("对不起!请上传图片!!");
return;
}
else
{
string fileName = ServerMapPath(_directory) + "\\" + DateTimeNowToString("yyyyMMddHHmmssfff") + rndNext(10, 99)ToString() + fileExt;
RequestFiles[0]SaveAs(fileName);
ResponseWrite("上传成功!");
}
}
}
catch
{
throw new Exception();
}
}
2 、C#文件下载
创建DownloadFile.aspx,在DownloadFile.aspx.cs键入如下方法:
/// <summary>
/// C#文件下载
/// </summary>
/// <param name="filename"></param>
public void MyDownload(string filename)
{
string path = ServerMapPath("/File/"+filename);
if(!FileExists(path))
{
ResponseWrite("对不起!文件不存在!!");
return;
}
SystemIOFileInfo file = new SystemIOFileInfo(path);
string fileFilt="asp|aspx|php|jsp|ascx|config|asa|"; //不可下载的文件,务必要过滤干净
string fileName = fileName;
string fileExt = fileNameSubstring(filenameLastIndexOf(""))Trim()ToLower();
if(fileFiltIndexOf(fileExt)!=-1)
{
ResponseWrite("对不起!该类文件禁止下载!!");
}
else
{
ResponseClear();
ResponseAddHeader("Content-Disposition", "attachment; filename=" + HttpUtilityUrlEncode(fileName));
ResponseAddHeader("Content-Length", fileLengthToString());
ResponseContentType = GetContentType(HttpUtilityUrlEncode(fileExt));
ResponseWriteFile(fileFullName);
ResponseEnd();
}
}
/// <summary>
/// 获取下载类型
/// </summary>
/// <param name="fileExt"></param>
/// <returns></returns>
public string GetContentType(string fileExt)
{
string ContentType;
switch (fileExt)
{
case "asf":
ContentType = "video/x-ms-asf"; break;
case "avi":
ContentType = "video/avi"; break;
case "doc":
ContentType = "application/msword"; break;
case "zip":
ContentType = "application/zip"; break;
case "xls":
ContentType = "application/vndms-excel"; break;
case "gif":
ContentType = "image/gif"; break;
case "jpg":
ContentType = "image/jpeg"; break;
case "jpeg":
ContentType = "image/jpeg"; break;
case "wav":
ContentType = "audio/wav"; break;
case "mp3":
ContentType = "audio/mpeg3"; break;
case "mpg":
ContentType = "video/mpeg"; break;
case "mepg":
ContentType = "video/mpeg"; break;
case "rtf":
ContentType = "application/rtf"; break;
case "html":
ContentType = "text/html"; break;
case "htm":
ContentType = "text/html"; break;
case "txt":
ContentType = "text/plain"; break;
default:
ContentType = "application/octet-stream";
break;
}
return ContentType;
}
*如何获取现有文件的ContentType属性
/// <summary>
/// 获取现有文件的ContentType属性
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
public string GetFileContentType(string filename)
{
string[] array = filenameSplit('');
string result = stringEmpty;
string suffix = "" + array[arrayLength - 1];
MicrosoftWinRegistryKey rg = MicrosoftWinRegistryClassesRootOpenSubKey(suffix);
object obj = rgGetValue("Content Type");
result = obj != null ? objToString() : stringEmpty;
rgClose();
return result;
}
来源:http://blog.csdn.net/pan_junbiao/article/details/7185382
标签:c#,文件,上传,下载
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring Native打包本地镜像的操作方法(无需通过Graal的maven插件buildtools)
2023-11-25 04:24:30
![](https://img.aspxhome.com/file/2023/9/59319_0s.jpg)
Android 存储路径选择方法
2022-04-03 21:05:42
SpringBoot MongoDB与MongoDB GridFS基本使用
2023-07-31 06:26:47
![](https://img.aspxhome.com/file/2023/7/77997_0s.png)
使用spring框架中的组件发送邮件功能说明
2022-12-29 03:53:55
![](https://img.aspxhome.com/file/2023/1/63341_0s.png)
C#记一次http协议multipart/form-data的boundary问题
2021-07-01 17:45:49
![](https://img.aspxhome.com/file/2023/3/86993_0s.png)
IDEA 如何控制编辑左侧的功能图标ICON(操作步骤)
2022-11-04 21:10:05
![](https://img.aspxhome.com/file/2023/8/128938_0s.jpg)
微信开发之使用java获取签名signature
2022-08-01 10:47:01
![](https://img.aspxhome.com/file/2023/3/65203_0s.jpg)
Android用PopupWindow实现自定义Dailog
2023-11-03 13:34:57
![](https://img.aspxhome.com/file/2023/0/138320_0s.jpg)
SpringBoot Security前后端分离登录验证的实现
2023-03-09 10:30:07
![](https://img.aspxhome.com/file/2023/8/71588_0s.png)
Android获取当前已连接的wifi信号强度的方法
2023-06-21 22:11:45
详解Android 通过Socket 和服务器通讯(附demo)
2023-05-03 01:08:22
![](https://img.aspxhome.com/file/2023/3/118973_0s.jpg)
C#设计模式之建造者模式生成器模式示例详解
2021-12-11 05:50:57
![](https://img.aspxhome.com/file/2023/7/83227_0s.jpg)
浅谈java中对集合对象list的几种循环访问
2022-02-11 02:32:33
C#实现利用Windows API读写INI文件的方法
2023-03-16 01:23:06
利用Spring Boot操作MongoDB的方法教程
2023-11-29 11:14:27
![](https://img.aspxhome.com/file/2023/5/60595_0s.jpg)
springMVC实现文件上传和下载
2023-04-17 16:39:07
Android线性布局与相对布局的实现
2021-07-26 09:08:01
![](https://img.aspxhome.com/file/2023/2/138552_0s.jpg)
浅析java移位符的具体使用
2023-12-21 09:36:13
java如何对map进行排序详解(map集合的使用)
2022-12-22 18:26:13
Java中的内部类你了解吗
2022-12-11 08:29:10
![](https://img.aspxhome.com/file/2023/0/62430_0s.png)