.net中型系统 第四章 实战项目开发文件篇之一
前言
本篇章主要讲解如何构建一个文件上传下载系统。
主要有三部分组成:
1、Common.UEditor:采用了开源的UEditor,主要能力是将客户终端上传文件转化为对应的字节流
2、Common.ClientProxy:主要能力是提供给第三方平台的对外上传方法
3、Common.FileService:上传下载文件站点,主要能力是实现文件的上传存储,下载等
下面让我们逐一讲解吧!!!
Common.UEditor
采用了开源的UEditor,主要能力是将客户终端上传文件转化为对应的字节流,主要的文件是Handler.cs和UploadHandler.cs
1、Handler.cs:该类是获取当前请求的上下文,并统一输出上传后的信息提醒,可被其他类继承,以便实现多样化的上传功能
public Handler(HttpContext context)
{
this.Request = context.Request;
this.Response = context.Response;
this.Context = context;
this.Server = context.Server;
}
public abstract void Process();
protected void WriteJson(object response)
{
string jsonpCallback = Request["callback"],
json = JsonConvert.SerializeObject(response);
if (String.IsNullOrWhiteSpace(jsonpCallback))
{
Response.AddHeader("Content-Type", "text/plain");
Response.Write(json);
}
else
{
Response.AddHeader("Content-Type", "application/javascript");
Response.Write(String.Format("{0}({1});", jsonpCallback, json));
}
Response.End();
}
2、UploadHandler.cs:继承自Handler.cs,该类实现的能力是将上传的文件转化为字节数组,以便后期处理,判断上传限制条件是否符合
public override void Process()
{
string Key = UploadConfig.Key;
long UserType = UploadConfig.UserType;
long UserID = UploadConfig.UserId;//UserHelp.GetUserID();
if (UserID <= 0)
{
Result.SetCode(SystemTip.UnLogin);
WriteResult();
return;
}
byte[] uploadFileBytes = null;
string uploadFileName = null;
if (UploadConfig.Base64)
{
uploadFileName = UploadConfig.Base64Filename;
uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
}
else
{
var file = Request.Files[UploadConfig.UploadFieldName];
uploadFileName = file.FileName;
if (file == null || file.ContentLength == 0)
{
Result.SetCode(SystemTip.NotFile);
WriteResult();
return;
}
if (!CheckFileType(uploadFileName))
{
Result.SetCode(SystemTip.TypeNotAllow);
Result.message = string.Format(Result.message, string.Join(",", UploadConfig.AllowExtensions));
WriteResult();
return;
}
if (!CheckFileSize(file.ContentLength))
{
Result.SetCode(SystemTip.SizeLimitExceed);
WriteResult();
return;
}
uploadFileBytes = new byte[file.ContentLength];
try
{
file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
}
catch (System.ArgumentOutOfRangeException)
{
Result.SetCode(SystemTip.ArgumentOutOfRangeException);
WriteResult();
return;
}
catch (System.ArgumentNullException)
{
Result.SetCode(SystemTip.ArgumentNullException);
WriteResult();
return;
}
catch (System.ArgumentException)
{
Result.SetCode(SystemTip.ArgumentException);
WriteResult();
return;
}
catch (System.IO.IOException)
{
Result.SetCode(SystemTip.IOException);
WriteResult();
return;
}
catch (System.NotSupportedException)
{
Result.SetCode(SystemTip.NotSupportedException);
WriteResult();
return;
}
catch (System.ObjectDisposedException)
{
Result.SetCode(SystemTip.ObjectDisposedException);
WriteResult();
return;
}
catch (Exception ex)
{
Result.SetCode(SystemTip.NetworkError);
WriteResult();
return;
}
}
this.Result = Upload(UserType, UserID, uploadFileName, uploadFileBytes);
WriteResult();
}
Common.ClientProxy
主要能力是提供给第三方平台的对外上传方法。
根据配置的第三方上传站点结合Common.UEditor转化的上传文件字节数组,形成输入参数至第三方上传站点处理,可以根据不同的需求上传或生成不同的文件。
类似下列源码就是上传图片的标准写法
public UploadResult UploadImage(long UserType, long UserID, string fileName, Stream fs)
{
var data = Common.PlatformMessage.JsonHelper.ObjectToJson(new { UserType = UserType.ToString(), UserID = UserID.ToString() });
var form = AddCheckData(data);
var result = HttpRequestHelper.GetFileByPost(FileConfig.Instance().Config.Url + "/upload/image", form, fs, "file", fileName, Encoding.UTF8);
UploadResult fileResult = new UploadResult();
if (result.Success)
{
fileResult = Common.PlatformMessage.JsonHelper.JsonToObject<UploadResult>(result.Message);
}
else
{
fileResult.message = result.Message;
fileResult.code = result.ReturnCode;
}
return fileResult;
}
Common.FileService
上传下载文件站点,主要能力是实现文件的上传存储,下载等。
该站点技术选型可以选择WebForm,MVC,根据你们项目的实际情况决定。
1、配置上传文件格式
2、实现上传文件逻辑,并存储至相应位置,返回至业务系统存储
3、记录日志
由于是一个完整的站点,贴代码出来不是很现实,大家可以尝试理解一些逻辑,后面也可以直接找我了解。
结语
通过以上三部曲我们可以构建一个比较完善的上传,下载文件系统,作为业务系统的其中一个模块,达到解耦,复用的目的。