欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-03-10 14:28:07
最近学习了 c#实现文件上传与下载,现在分享给大家。 1、c#文件上传 创建myupload.htm页面,用于测试

最近学习了 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; 
}  

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