H5文件上传2

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;

public class Handler : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{

string otype = context.Request["otype"].ToString();

switch (otype)
{


case "UploadTest":
UploadTest(context, otype);
break;

}
}

public void UploadTest(HttpContext context, string otype)
{

int UID = 10000;
if (otype == "UploadTest")
{
string FileName = HttpContext.Current.Request.Files["Filedata"].FileName;
if (0 != FileName.Length)
{
string FileEx = FileName.Substring(FileName.LastIndexOf(".") + 1);//获取后缀名
//做后缀名判断
string webfilepath = "";
int Fid = SiteSaveProFile2(UID, 0, "测试上传", context.Request["modulename"], FileEx, "Filedata", ProUtil.FileUtil_FileRoot, ProUtil.FileUtil_WebFileRoot, ref webfilepath, context);
if (Fid > 0)
{
context.Response.Write(ProUtil.SuccessAlert(200, "上传成功", null, null, null, null, ","data":"" + Fid + "," + webfilepath + """));
}
else
{
context.Response.Write(ProUtil.ErrorAlert(300, "保存附件出错!"));
}
}
context.Response.End();
return;
}

}

public static int SiteSaveProFile2(int UID, int OID, string OTable, string ModuleName, string FileEx, string FileControlName, string SiteFileRoot, string SiteWebRoot, ref string webfilepath, HttpContext context)
{
DateTime nowTime = DateTime.Now;
string UName = CKXD.ShopProj.BLL.sys_staff.GetNames(UID.ToString());
//保存文件

string FilePath = "";
T9.Util.FileUtil.ExistsOrCreate(FilePath, SiteFileRoot, context);
FilePath += "/" + UName + "_" + UID;
T9.Util.FileUtil.ExistsOrCreate(FilePath, SiteFileRoot, context);
FilePath += "/" + ModuleName;
T9.Util.FileUtil.ExistsOrCreate(FilePath, SiteFileRoot, context);
FilePath += "/" + nowTime.ToString("yyyy-MM");
T9.Util.FileUtil.ExistsOrCreate(FilePath, SiteFileRoot, context);
FilePath += "/" + nowTime.ToString("yyyy-MM-dd");
T9.Util.FileUtil.ExistsOrCreate(FilePath, SiteFileRoot, context);
string FileName = Guid.NewGuid().ToString();
FilePath += "/" + FileName + "." + FileEx;
webfilepath = SiteWebRoot + FilePath;
HttpContext.Current.Request.Files[FileControlName].SaveAs(context.Server.MapPath(SiteFileRoot + FilePath));

//文件信息入库

CKXD.ShopProj.Model.sys_files sysFile = new CKXD.ShopProj.Model.sys_files();
CKXD.ShopProj.BLL.sys_files sysFileBLL = new CKXD.ShopProj.BLL.sys_files();
sysFile.F_FilePath = FilePath;
sysFile.F_FileType = FileEx;
sysFile.F_Module = ModuleName;
sysFile.F_Table = OTable;
sysFile.F_ID = OID;
sysFile.F_FileIsExist = 0;
sysFile.UpdateTime = nowTime;
sysFile.UpdateUser = UID;
sysFile.CreateTime = nowTime;
sysFile.CreateUser = UID;
return sysFileBLL.Add(sysFile);

}

public bool IsReusable
{
get
{
return false;
}
}

}

原文地址:https://www.cnblogs.com/wugh8726254/p/13957513.html