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; } 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。 上一篇: C# winform 模拟键盘输入自动接入访问网络的实例 下一篇: 历史上皇家礼宴是什么样的?规格有多高? 推荐阅读 使用smartupload组件实现jsp+jdbc上传下载文件实例解析 vue-cli+axios实现文件上传下载功能(下载接收后台返回文件流) 基于Java文件输入输出流实现文件上传下载功能 PHP实现文件上传与下载实例与总结 Spring Boot + thymeleaf 实现文件上传下载功能 HTML5拖拽文件到浏览器并实现文件上传下载功能代码 C#文件上传与下载的实现方法 C#实现文件上传下载Excel文档示例代码 C#实现文件上传及文件下载功能实例代码 C# 文件上传下载(Excel导入,多线程下载)功能的实现代码