C#——文件上传(一般处理程序ashx)
Framework版本:.Net Framework 4
1、FileInfo实体
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MongoDB.Bson; using ReligionServer.util; using ReligionServer.constant;namespace ReligionServer.Model {//为什么会有多余的get set方法,因为测试写的,没有删//封装的一个文件类, 文件基本信息public class FileInfo{public ObjectId _id;public String FileCode { get; set; }//目前还没有研究出ObjectId序列化的最好解决方式, 所以暂时使用FileCode替代Idpublic String Name { get; set; }public String Type { get; set; }public String Desc { get; set; }public String Path { get; set; }public int Access { get; set; }public String ForeignKey { get; set; }public DateTime CreateTime { get; set; }public String TypeCode { get; set; }//辅助字段public FileInfo() { }public void set_Id(ObjectId id){this._id = id;}public ObjectId get_Id(){return this._id;}public void setFileCode(){this.FileCode = CommonUtil.CreateId();}public String getFileCode(){return this.FileCode;}public void setName(String name){this.Name = name;}public String getName(){return this.Name;}public void setType(String type){this.Type = FileTypeConstant.GetType(type);}public String getType(){return this.Type;}public void setDesc(String desc){this.Desc = desc;}public String getDesc(){return this.Desc;}public void setPath(String path){this.Path = path;}public String getPath(){return this.Path;}public void setAccess(int access){this.Access = access;}public int getAccess(){return this.Access;}public void setForeignKey(){this.ForeignKey = CommonUtil.CreateId();}public String getForeignKey(){return this.ForeignKey;}public void setCreateTime(){this.CreateTime = DateTime.Now;}public DateTime getCreateTime(){return this.CreateTime;}public void setTypeCode(String code){this.TypeCode = code;}public String getTypeCode(){return this.TypeCode;}} }
2、Handler具体实现
using System;using System.IO; using System.Collections.Generic; using System.Linq; using System.Web; using ReligionServer.util; using ReligionServer.Model; using ReligionServer.service; using System.Text.RegularExpressions;namespace ReligionServer.handler {/// <summary>/// FileHandler 的摘要说明/// 处理文件请求/// </summary>public class FileHandler : BaseHandler, IHttpHandler{private FileInfoService fileInfoService = new FileInfoService();private readonly String targetSavePath = "~/ReligionFile/";public void ProcessRequest(HttpContext context){base.InitAction(context);}#region 文件上传//和第三方表关联的文件上传初始化逻辑public void Upload_Foreign(HttpContext context){Model.FileInfo fileInfo = InitFileInfo(context, 0);if (CommonUtil.IsEmpty(fileInfo.getForeignKey())){NoParams<Model.FileInfo>(context, "关联外键为空, 还请检测关联属性是否上传成功, 外键是否为空");}else if (base.IsEmpty(fileInfo.getTypeCode())){fileInfo.setTypeCode("0");NoParams<Model.FileInfo>(context, "TypeCode is null or '' ...");}else{List<Model.FileInfo> uploadFileList = FileInsert(context, InitFileInfo(context, 0));if (uploadFileList.Count == 0){Error<String>(context, new List<String>() { "上传失败: 后台获取文件信息失败" });}else{int errorCount = (int)context.Items["FileUploadErrorCount"];int successCount = uploadFileList.Count - errorCount;Success<String>(context, new List<String>() { "一共上传 " + uploadFileList.Count + "个文件,其中成功 " + successCount + "个,失败" + errorCount + "个" });}}}//文件上传初始化逻辑public void Upload(HttpContext context){Model.FileInfo fileInfo = InitFileInfo(context, 1);if (base.IsEmpty(fileInfo.getTypeCode())){fileInfo.setTypeCode("0");NoParams<Model.FileInfo>(context, "TypeCode is null or '' ...");}else {List<Model.FileInfo> uploadFileList = FileInsert(context, fileInfo);if (uploadFileList.Count == 0){Error<String>(context, new List<String>() { "上传失败: 后台获取文件信息失败" });}else{int errorCount = (int)context.Items["FileUploadErrorCount"];int successCount = uploadFileList.Count - errorCount;Success<String>(context, new List<String>() { "一共上传 " + uploadFileList.Count + "个文件,其中成功 " + successCount + "个,失败" + errorCount + "个" });}}}/// <summary>/// 供类相互调用的文件上传/// </summary>/// <param name="context"></param>/// <param name="foreignKey"></param>/// <returns></returns>public List<Model.FileInfo> Upload_Invoke(HttpContext context, String foreignKey) {if (!base.IsEmpty(foreignKey)) {Model.FileInfo fileInfo = InitFileInfo(context, 0);fileInfo.ForeignKey = foreignKey;List<Model.FileInfo> uploadFileList = FileInsert(context, fileInfo);return uploadFileList;}return new List<Model.FileInfo>();}//执行文件转存以及数据库记录插入的方法private List<Model.FileInfo> FileInsert(HttpContext context, Model.FileInfo fileInfo){List<Model.FileInfo> fileInfoList = new List<Model.FileInfo>();//List<Model.FileInfo> fileInfoList = new List<Model.FileInfo>();String directoryName = DateUtil.CurrentDateTimeValue();//文件夹名称//HttpContext.Current.Request.PhysicalApplicationPath 获取当前正在执行的服务器应用程序的根目录的物理文件系统路径String targetPhysicalFilePath = HttpContext.Current.Request.PhysicalApplicationPath + "ReligionFile/" + directoryName;//不存在就创建if (!Directory.Exists(targetPhysicalFilePath)){Directory.CreateDirectory(targetPhysicalFilePath);}//获取上传文件集合HttpFileCollection fileCollection = context.Request.Files;//HttpPostedFile temp = context.Request.Files["regfile"];//测试//String tempe = context.Request.Params["regfile"];//HttpPostedFileWrapper fileWrapper = context.Request.Files[0]; Model.FileInfo tempFileInfo = null;if (fileCollection.Count > 0){HttpPostedFile item = null;for (int i = 0; i < fileCollection.Count; i++){item = fileCollection[i];String suffix = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];//获取文件的后缀名tempFileInfo = new Model.FileInfo();tempFileInfo = (Model.FileInfo)BeanUtil.PropCopy(fileInfo, tempFileInfo);//tempFileInfo.set_Id(CommonUtil.CreateObjectId());这样设置Id是无效的tempFileInfo.setName(item.FileName);//存的是以前的文件名, 是否有意义 tempFileInfo.setFileCode();tempFileInfo.setType(suffix);tempFileInfo.setCreateTime();suffix = "." + suffix;if (suffix.ToLower().Equals(".txt")){suffix = ".doc";//这里是否有必要? 这是个问题 }String realFileName = Guid.NewGuid().ToString() + suffix;tempFileInfo.setTypeCode(tempFileInfo.getTypeCode() + ":" + "ReligionFile/" + directoryName + "/" + realFileName);tempFileInfo.setPath("ReligionFile/" + directoryName + "/" + realFileName);item.SaveAs(context.Server.MapPath(targetSavePath + directoryName + "/" + realFileName));//文件转存 必须是文件的根目录, 而且不能使虚拟目录 fileInfoList.Add(tempFileInfo);}//foreach (String key in fileCollection) {//这里前台只有一个文件选择框, 那么多文件时keys都是相同的,// HttpPostedFile item = fileCollection[key];// String suffix = item.FileName.Split('.')[item.FileName.Split('.').Length - 1];//获取文件的后缀名// fileInfo.setName(item.FileName);// fileInfo.setFileCode();// fileInfo.setType(suffix);// fileInfo.setCreateTime();// suffix = "." + suffix;// if (suffix.ToLower().Equals(".txt")) {// suffix = ".doc";//这里是否有必要? 这是个问题// }// String realFileName = Guid.NewGuid().ToString() + suffix;// fileInfo.setTypeCode(fileInfo.getTypeCode() + ":" + "ReligionFile/" + directoryName + realFileName);// item.SaveAs(context.Server.MapPath(targetSavePath + directoryName + "/" + realFileName));//文件转存 必须是文件的根目录, 而且不能使虚拟目录// fileInfoList.Add(fileInfo);//}int errorCount = fileInfoService.InsertBatchFileInfo(fileInfoList);//文件信息批量入表, 返回失败个数context.Items.Add("FileUploadErrorCount", errorCount);}return fileInfoList;}/// <summary>/// Base64格式的图片上传/// </summary>/// <param name="context"></param>public void Insert_Base64_Img(HttpContext context){vo.Base64Image base64Image = base.GetInstance<vo.Base64Image>(context, new vo.Base64Image());if (base.IsEmpty(base64Image.Base64String)){NoParams<vo.Base64Image>(context, "上传参数存在空值");}else{List<Model.FileInfo> fileInfoList = util.ImageUtil.Base64ImageInsertBatch(context, util.ArrraysUtil.PurifyArrays<String>(Regex.Split(base64Image.Base64String, "c#", RegexOptions.IgnoreCase)));if (fileInfoList.Count > 0){int errorCount = fileInfoService.InsertBatchFileInfo(fileInfoList);int successCount = fileInfoList.Count - errorCount;Success<String>(context, new List<String>() { "一共上传 " + fileInfoList.Count + "个文件,其中成功 " + successCount + "个,失败" + errorCount + "个" });}else{Error<String>(context, new List<String>() { "上传失败: 后台将Base64转存时失败" });}}}#endregion#region 文件删除/// <summary>/// 根据ObjectId删除指定的文件信息/// </summary>/// <param name="context"></param>public void Del_ObjectId(HttpContext context){//暂时不做 }/// <summary>/// 根据FileCode删除指定的文件信息/// </summary>/// <param name="context"></param>public void Del_FileCode(HttpContext context){Model.FileInfo fileInfo = this.InitFileInfo(context, -1);fileInfo = fileInfoService.FindFileInfoByFileCode(fileInfo.getFileCode());if (fileInfo != null){if (fileInfoService.RemoveFileInfoByFileCode(fileInfo.getFileCode())){RemoveLocalFile(fileInfo);Success<Model.FileInfo>(context, new List<Model.FileInfo>() { fileInfo });}else{Error<Model.FileInfo>(context, new List<Model.FileInfo>() { fileInfo });}}else{NoParams<Model.FileInfo>(context, "该条记录不存在,无法删除");}}/// <summary>/// 根据外键ForeignKey删除所关联的文件信息/// </summary>/// <param name="context"></param>public void Del_ForeignKey(HttpContext context){}/// <summary>/// 根据传递进来的FileInfo的path字段删除服务器上对应的文件/// </summary>/// <param name="fileInfo"></param>private void RemoveLocalFile(Model.FileInfo fileInfo){System.Diagnostics.Debug.WriteLine(HttpContext.Current.Request.PhysicalApplicationPath + fileInfo.getPath());//FileUtil.RemoveFile(context.Request.PhysicalApplicationPath + fileInfo.getPath());FileUtil.RemoveFile(HttpContext.Current.Request.PhysicalApplicationPath + fileInfo.getPath());}#endregion#region 文件修改/// <summary>/// 根据FileCode修改指定的文件信息/// </summary>/// <param name="context"></param>public void Update_FileCode(HttpContext context){Model.FileInfo target = InitFileInfo(context, -1);String fileCode = target.getFileCode();List<Model.FileInfo> list = null;if (CommonUtil.IsEmpty(target.getForeignKey()) || CommonUtil.IsEmpty(target.getFileCode())){NoParams<Model.FileInfo>(context, "关联外键ForeignKey或FileCode为空");}else{list = FileInsert(context, InitFileInfo(context, 0));}if (null != list){target = list[0];target.FileCode = fileCode;Model.FileInfo source = fileInfoService.FindFileInfoByFileCode(target.getFileCode());if (null != source){target._id = source._id;target.Access = source.Access;target.ForeignKey = source.ForeignKey;if (fileInfoService.UpdateAllByFileCode(target)){RemoveLocalFile(source);//如果上传成功, 就将原来的文件删除Success<Model.FileInfo>(context, new List<Model.FileInfo>() { fileInfoService.FindFileInfoByFileCode(target.FileCode) });}else{Error<Model.FileInfo>(context, new List<Model.FileInfo>() { target });RemoveLocalFile(target);//如果更新失败, 就将上传的文件删除 }}else{Init<Model.FileInfo>(context, "300", "", new List<Model.FileInfo>() { });}}}#endregion#region 文件查找/// <summary>/// 根据FileCode查找指定的文件信息/// </summary>/// <param name="context"></param>public void Query_FileCode(HttpContext context){Model.FileInfo fileInfo = fileInfoService.FindFileInfoByFileCode(this.InitFileInfo(context, -1).FileCode);if (fileInfo == null){NoParams<Model.FileInfo>(context, "该记录不存在");}else{Success<Model.FileInfo>(context, new List<Model.FileInfo>() { fileInfo });}}/// <summary>/// 根据ForeignKey查询所有的文件信息/// </summary>/// <param name="context"></param>public void Query_Foreign(HttpContext context){//Model.FileInfo info = this.InitFileInfo(context, -1);List<Model.FileInfo> list = fileInfoService.FindFileInfoListByForeignKey(this.InitFileInfo(context, -1).ForeignKey);//if (list == null || list.Count == 0) {// NoParams<Model.FileInfo>(context, "没有匹配的记录");//} else {// Success<Model.FileInfo>(context, list);//}Success<Model.FileInfo>(context, list);}#endregion//初始化FileInfoprivate Model.FileInfo InitFileInfo(HttpContext context, int access){Model.FileInfo fileInfo = ParametersUtil.GetInstanceFormRequest<Model.FileInfo>(context, new Model.FileInfo());//这里主要是获取ForeignKeyif (access != -1){fileInfo.setAccess(access);//设置文件资源类型 {0表示关联; 1表示独立资源,没有关联} -1表示不作处理 }return fileInfo;}public bool IsReusable{get{return false;}}} }
转载于:https://www.cnblogs.com/threadj/p/10536336.html
C#——文件上传(一般处理程序ashx)相关推荐
- swfupload 多文件上传的属性与事件方法总结
SWFUpload的原理: 利用Flash选择文件后上传,通过Flash和JS交互,对整个过程进行控制--包括页面的DOM操作之类的,都可以通过JS来进行控制. 说白了,就是用Flash上传,JS操作 ...
- 通过一般处理程序实现【文件上传】
注意事项: 1>表单的提交方式必须为post,method="post";//由于http上面的数据大小有限,所以不支持使用get显示到上面. 2>必须修改表中提交数据 ...
- jquery文件上传插件 uploadify java_jQuery文件上传插件Uploadify使用指南
对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持 Flash,主要特性:支持多文件上传.HTML5版本可拖拽上传.实时上传进度条显示.强大的参数 定制功能,如文件大小. ...
- UEditor 任意文件上传漏洞
1 漏洞简介 1.1 漏洞描述 Ueditor是百度开发的一个网站编辑器,目前已经不对其进行后续开发和更新,该漏洞只存在于该编辑器的.net版本.其他的php,jsp,asp版本不受此UEditor的 ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)
相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来实现呢?我并不否认"拿来主义",只是我个人 ...
- 文件上传漏洞 (上传知识点、题型总结大全-upload靶场全解)
文件上传漏洞 什么是文件上传漏洞 什么是webshell 一句话木马大全 产生文件上传漏洞的原因 文件上传漏洞的攻击与防御方式 1.前端限制 2.检查扩展名 1.黑名单策略, 2.白名单策略 3.检查 ...
- php ajaxfileupload.js 使用,ajaxfileupload.js实现文件上传(附步骤代码)
这次给大家带来ajaxfileupload.js实现文件上传(附步骤代码),ajaxfileupload.js实现文件上传的注意事项有哪些,下面就是实战案例,一起来看一下. AjaxUpLoad.js ...
- 大文件上传Jquery 插件Uploadify-v2.1.4使用图解
官方下载 官方文档 官方演示 1.:首先从官网下载最近版uploadify插件,目前为v2.1.4版本: 2 :新建项目,或者在自己项目中加入下载文件jquery.uploadify-v2.1.4,再 ...
- AjaxUpLoad.js使用实现文件上传
AjaxUpLoad.js的使用实现无刷新文件上传,如图. 图1 文件上传前 图2 文件上传后 1.创建页面并编写HTML 上传文档: <div class="uploadFile&q ...
最新文章
- 继续说说美国互联网版权监管法案(SOPA)
- laravel 模版引擎使用
- golang的WaitGroup
- Java认证授权框架Spring Security介绍
- 京东金融回应“白条漏洞”:在2017年已修复
- 去除input的自动填充色
- 商业楼与写字楼的区别详解
- ubuntu 显卡驱动崩掉导致分辨率异常的问题解决
- 电容器的 ESR 参数
- Python编程好不好学?入门难吗?
- 云服务器(CentOS 7)上面部署Node.js环境
- 我为什么要考非全日制研究生
- java list clear 垃圾回收_Java垃圾回收
- 贝塞尔曲线 三维 拼接 matlab,贝塞尔曲线公式
- 电位器的主要参数有哪些?
- 景区自定义手绘地图叠加
- jpa查询表的部分字段
- ModuleNotFoundError: No module named ‘_bz2‘
- 中级职称聘用计算机模块,中级职称计算机模块有关问题
- TCP服务器的学生管理系统
热门文章
- 哪本python入门书内容最详细-重磅 | 由浅入深的 AI 学习路线,最详细的资源整理!...
- python自学需要哪些基础知识-零基础学Python应该学习哪些入门知识及学习步骤安排...
- python下载安装教程2.7-Python2.7.6下载
- python基础教程攻略-python基础教程(一)
- python编辑器安卓下载-Python的下载安装与Python编辑器的安装
- python turtle画气球-micro:bit + LoRa 实现气球追踪
- python初学者代码-Python-为什么Python是初学者的完美选择?
- python常见错误-Python 常见报错类型
- python学费多少-2020年10月徐州学python要多少学费
- 如何用python画出中国地图-用Python画一个中国地图