C#实现的文件上传下载工具类完整实例【上传文件自动命名】
作者:jianyuerensheng 时间:2023-03-05 17:39:38
本文实例讲述了C#实现的文件上传下载工具类。分享给大家供大家参考,具体如下:
这里给出的工具类是在VS2013环境下采用C#语言实现文件上传、下载功能。上传时,为避免文件名在服务器中重复,采用“服务器时间+8位随机码+文件名+文件后缀“的方式作为服务器上的文件名;下载采用的是WebAPI的方式进行的,下载成功后可自定义文件的保存路径。
具体源码如下所示:
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using JYRS.Util;
namespace JYRS.Utils
{
public class FileHelper
{
/// <summary>
/// 将文件名解析成文件的上传路径
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="path">文件路径</param>
/// <returns path>文件在服务器上的路径</returns>
public static String transPath(string fileName, string path)
{
createDir(path);
//取服务器时间+8位随机码作为部分文件名,确保文件名无重复
string nowStr = DateTime.Now.ToString("yyyyMMddhhmmssff") + Global.CreateRandomCode(8);
// 去掉后缀的文件名
string fileNameStr = fileName.Substring(0, fileName.LastIndexOf("."));
// 文件后缀
String suffix = fileName.Substring(fileName.LastIndexOf(".") + 1);
if (fileName.Trim() != "")
{
// 如果名称不为"",说明该文件存在,否则说明该文件不存在
path += "\\" + fileNameStr + nowStr + "." + suffix;// 定义上传路径
}
return path;
}
/// <summary>
/// 创建文件目录
/// </summary>
/// <param name="root">根目录</param>
/// <returns ></returns>
private static void createDir(String root)
{
// 检查目录
if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(root)))
{
Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(root));
}
}
/// <summary>
/// 根据文件在服务器上的路径下载文件,此处采用的是WebAPI的方式进行文件下载,下载成功后可自定义文件的保存路径
/// </summary>
/// <param name="fileName">文件名</param>
/// <param name="path">文件路径</param>
/// <returns></returns>
public static HttpResponseMessage download(string fileName, string path)
{
try
{
var stream = new FileStream(path, FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName
};
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
}
}
Controller层调用类
[HttpGet]
public HttpResponseMessage UploadAndDownload()
{
//文件上传到服务器上的根目录
string root = System.Web.Hosting.HostingEnvironment.MapPath(@"~/upload");
string fileName = "测试.docx";
//解析文件在服务器上的上传路径
string path = FileHelper.transPath(fileName, root);
//获取要上传的文件
var files = HttpContext.Current.Request.Files;
HttpPostedFile file = HttpContext.Current.Request.Files[0];
//保存文件
file.SaveAs(System.Web.HttpContext.Current.Server.MapPath(path));
//下载word文件
return FileHelper.download(fileName, path);
}
view层:
<a href="http://localhost:60179/api/CeshiController/UploadAndDownload" rel="external nofollow" "> 导出 </a>
希望本文所述对大家C#程序设计有所帮助。
来源:http://blog.csdn.net/jianyuerensheng/article/details/78184730
标签:C#,文件,工具类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决MyEclipse出现the user operation is waiting的问题
2022-05-02 21:44:07
![](https://img.aspxhome.com/file/2023/0/64750_0s.gif)
Java Validation方法入参校验实现过程解析
2021-08-04 03:31:50
Java数据结构超详细分析二叉搜索树
2022-12-01 01:34:20
![](https://img.aspxhome.com/file/2023/3/61023_0s.jpg)
Eclipse代码格式化设置简单介绍
2023-10-26 21:59:31
Java中的多态用法实例分析
2021-07-11 18:36:35
![](https://img.aspxhome.com/file/2023/8/77098_0s.png)
Java反射机制详解
2023-10-01 12:18:04
Java Base64解码错误及解决方法
2023-02-09 03:36:35
![](https://img.aspxhome.com/file/2023/5/66515_0s.jpg)
C#单例类的实现方法
2023-12-01 02:13:30
Android 顶部标题栏随滑动时的渐变隐藏和渐变显示效果
2023-11-26 07:48:39
![](https://img.aspxhome.com/file/2023/4/102574_0s.gif)
Android 获取drawable目录图片 并存入指定文件的步骤详解
2021-11-01 07:04:48
Java设计模式之监听器模式实例详解
2022-10-04 02:51:44
![](https://img.aspxhome.com/file/2023/8/72568_0s.png)
基于C#实现图片滑动验证码的示例代码
2022-01-15 22:53:31
![](https://img.aspxhome.com/file/2023/3/110173_0s.jpg)
spring cloud gateway请求跨域问题解决方案
2021-11-05 11:19:25
Java中实现在一个方法中调用另一个方法
2022-05-15 10:02:11
java中 Set与Map排序输出到Writer详解及实例
2022-05-01 07:23:56
![](https://img.aspxhome.com/file/2023/3/96823_0s.png)
java利用mybatis拦截器统计sql执行时间示例
2021-08-22 11:35:41
教你轻松制作java音乐播放器
2023-09-27 11:17:11
![](https://img.aspxhome.com/file/2023/0/91670_0s.jpg)
c# DataDirectory的用法
2021-07-30 14:36:22
Java线程公平锁和非公平锁的差异讲解
2022-06-26 09:59:09
详解Java String字符串获取每一个字符及常用方法
2022-12-14 05:15:33
![](https://img.aspxhome.com/file/2023/6/85486_0s.jpg)