栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C# > C#教程

C#实现文件上传与下载功能实例

C#教程 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C#实现文件上传与下载功能实例

最近学习了 C#实现文件上传与下载,现在分享给大家。

1、C#文件上传

创建MyUpload.htm页面,用于测试

 

创建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键入如下方法:

///  
/// C#文件下载 
///  
///  
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(); 
  } 
} 
 
///  
/// 获取下载类型 
///  
///  
///  
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属性

///  
/// 获取现有文件的ContentType属性 
///  
///  
///  
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; 
}  

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/123892.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号