using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using LitJson;
using LY.CCPC.Model;
using System.IO;
using System.Reflection;
using System.Data;
using System.Configuration;
using System.Collections.Specialized;
using LY.CCPC.BLL;
using LY.CCPCUEP.Utility; namespace LY.CCPCUEP.UI.Handlers
{
/// <summary>
/// ImportFileHandler 的摘要说明
/// </summary>
public class ImportFileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
JsonObject json = new JsonObject() { Success = true }; string fullMethod = context.Request.QueryString["mod"];
string strType = context.Request.QueryString["type"];
string fullFileName = string.Empty;
FTPHelper ftp=getFtpPath();
try
{
HttpFileCollection fileColl = context.Request.Files;
string empid = context.User.Identity.Name;
if ("UPLOAD".Equals(fullMethod.ToUpper()))
{
HttpPostedFile file = fileColl[];//得到上传的文件 //1.判断文件扩展名是否符合要求
string org_file_name = getOrgFileName(file.FileName);//初始文件名
string ext = getFileExtName(file.FileName);
ext = ext.ToLower();
string[] exts = new string[] { ".jpg", ".ppt", ".pptx", ".bmp", ".gif", ".png", ".jpeg", ".txt", ".csv", ".xls", ".xlsx", ".doc", ".docx", ".pdf", ".avi ", ".rmvb ", ".rm ", ".asf ", ".divx ", ".mpg ", ".mpeg ", ".mpe ", ".wmv ", ".mp4 ", ".mkv ", ".vob", ".rar", ".zip" };
if (!exts.Contains(ext))
{
json.Success = false;
json.Data = "";
json.Message = string.Format("系统不允许上传类型为{0}的文件,请重新选择上传!", ext);
// context.Response.Write(JsonMapper.ToJson(json));
return;
} //2.判断文件大小,不能超过5MB
int fileSize = file.ContentLength;
const int MB = * ; if (fileSize / MB >= )
{
json.Success = false;
json.Data = "";
json.Message = "上传的文件不能超过5MB,请重新选择上传!";
//context.Response.Write(JsonMapper.ToJson(json));
return;
} string filePath = this.getLocalPath("");
string uploadPath = context.Server.MapPath("~" + filePath) ;//导入文件路径
string fileName = ""; fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff")+ ext;//文件名
if (!Directory.Exists(uploadPath))
{
Directory.CreateDirectory(uploadPath);
}
fullFileName = uploadPath +"\\"+ fileName;
file.SaveAs(fullFileName);//文件在服务器的全路径 ftp.Upload(fullFileName); // 上传到FTP服务器
string file_path = ftp.ftpRemotePath.Substring(ftp.ftpRemotePath.IndexOf('/')) ;//保存在服务器的路径
string org_file_path = (file_path + "\\" + fileName).Replace("\\", "/");
org_file_name = org_file_name.Substring(org_file_name.LastIndexOf("\\") + );
string strMsg = "{\"file_path\":\""+org_file_path+"\",\"file_name\":\""+org_file_name+"\"}";
//string strMsg = string.Format("[{\"file_path\":\"{0}\"},{\"file_name\":\"{1}\"}]", , );
json.Data = strMsg; }
}
catch (Exception ex)
{
json.Success = false;
json.Message = ex.Message;
json.Data = ex.Message + "###上传FTP路径:" + ftp.ftpURI;
}
finally
{
string result = JsonMapper.ToJson(json);
//删除AP上的临时文件
if (File.Exists(fullFileName))
{
File.Delete(fullFileName);
}
context.Response.Write(result);
context.Response.End();
}
} private string getLocalPath(string flag)
{
string path = string.Empty; string ftpPath = ConfigurationManager.AppSettings["FTP"];
string filePath = ConfigurationManager.AppSettings["uploadPath"];
string ImportPath = ConfigurationManager.AppSettings["ImportPath"];
string ExportPath = ConfigurationManager.AppSettings["ExportPath"];
if ("".Equals(flag))
{
path = ftpPath + filePath;
}
else if ("".Equals(flag))
{
path = ftpPath + ImportPath;
}
else if ("".Equals(flag))
{
path = ftpPath + ExportPath;
}
return path;
}
/// <summary>
/// 返回FTP对像
/// </summary>
/// <returns></returns>
private FTPHelper getFtpPath()
{ string ftpPath = ConfigurationManager.AppSettings["FTPServer"];
string filePath = ConfigurationManager.AppSettings["FTPPath"];
string ftpUser = ConfigurationManager.AppSettings["FTPUser"];
string ftpPass = ConfigurationManager.AppSettings["FTPPass"]; return new FTPHelper(ftpPath,filePath,ftpUser,ftpPass);
} /// <summary>
/// 获取文件扩展名
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private string getFileExtName(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return string.Empty;
if (!fileName.Contains(".")) return fileName;
int posIndex = fileName.LastIndexOf('.');
return fileName.Substring(posIndex);
}
/// <summary>
/// 获取文件原始名
/// </summary>
/// <param name="fileName"></param>
/// <returns></returns>
private string getOrgFileName(string fileName)
{
if (string.IsNullOrEmpty(fileName)) return string.Empty;
if (!fileName.Contains("/"))
return fileName;
int posIndex = fileName.LastIndexOf('/')+;
return fileName.Substring(posIndex);
}
public bool IsReusable
{
get
{
return false;
}
} }
}

ImportFileHandler 附件上传的更多相关文章

  1. asp.net结合uploadify实现多附件上传

    1.说明 uploadify是一款优秀jQuery插件,主要功能是批量上传文件.大多数同学对多附件上传感到棘手,现将asp.net结合uploadfiy如何实现批量上传附件给大家讲解一下,有什么不对的 ...

  2. ueditor调用其中的附件上传功能

    ueditor实际上是集成了webuploader, 在做内容发布的时候想既有ueditor又有单独的附件上传按钮,这时再加载一个webuploader就显得过于臃肿了,单独利用ueditor的上传功 ...

  3. 基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用

    大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...

  4. 百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程之更改图片和附件上传路径

    本文是接上一篇博客,如果有疑问请先阅读上一篇:百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程 默认UEditor上传图片的路径是,编辑器包目录里面的net目录下 下面就演示如 ...

  5. tp中附件上传文件,表单提交

    public function tianjia(){ $goods=D('Goods'); if(!empty($_POST)){ if($_FILES['f_goods_image']['error ...

  6. 使用plupload做一个类似qq邮箱附件上传的效果

    公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...

  7. Dynamic CRM 2013学习笔记(十三)附件上传 / 上传附件

    上传附件可能是CRM里比较常用的一个需求了,本文将介绍如何在CRM里实现附件的上传.显示及下载.包括以下几个步骤: 附件上传的web页面 附件显示及下载的附件实体 调用上传web页面的JS文件 实体上 ...

  8. playframework中多附件上传注意事项

    playframework中多附件上传注意事项 2013年09月24日 play 暂无评论 //play版本问题 经确认,1.0.3.2版本下控制器中方法参数  List<File> fi ...

  9. JS实现多附件上传(asp.net)

    前几天,用户提出一个需求-多附件上传,另外,每个上传文件要加一个别名,本人创新少,从网上收集了资料,稍微改写,满足了 客户的需求.在应用到程序之前,先做了个小测试,测试通过,小高兴,就记录下了这个小测 ...

随机推荐

  1. Yocto和Android编译命令的简化和自动完成的实现

    简化编译命令 无论是在Android编译系统中,还是在Yocto编译系统中,要编译一个目标,输入命令都有点费事. Yocto系统: source setup-environment $FOLDER b ...

  2. cesium 飞线 瓣体传感器(雷达扫描) 效果

    参考:github地址 本人新手,npm webpack 这些还是一知半解,只记录自己得到成功结果的操作步骤,可能存在多余或错误的步骤. 1.github 把代码下载下来,解压. 2.webstorm ...

  3. Python_tkinter(5)_GUI工具

    1.Excel行数统计工具 源代码 import xlrd import tkinter as tk import tkinter.filedialog # 计算一个工作簿中所有Sheet表的行数 d ...

  4. Ubuntu18.04 快速返回桌面 【快捷键】

    设置->设备->键盘->窗口->隐藏窗口 :双击重置快捷键

  5. windows下安装Mongodb的方法

    参考:https://blog.csdn.net/heshushun/article/details/77776706

  6. idea搭建可运行Servlet的Web项目[maven]

    1. new Project File > new > Project… 2. 填写 GroupID\ArtifactID GroupID 是项目组织唯一的标识符,实际对应JAVA的包的结 ...

  7. mysql 的mgr集群

    mysql 的mgr集群 http://wubx.net/mgr%E7%9B%91%E6%8E%A7%E5%8F%8A%E4%BC%98%E5%8C%96%E7%82%B9/ MGR调优参数因为基本复 ...

  8. 20175211 2018-2019-2 《Java程序设计》第五周学习总结

    目录 教材学习内容总结 第六章 接口与实现 教材学习中的问题和解决过程 代码调试中的问题和解决过程 代码托管 上周考试错题总结 学习进度条 参考资料 教材学习内容总结 第六章 接口与实现 6.1 接口 ...

  9. 记AOP概念理解

    OOD/OOP面向名词领域,AOP面向动词领域. 应用举例 假设有在一个应用系统中,有一个共享的数据必须被并发同时访问,首先,将这个数据封装在数据对象中,称为Data Class,同时,将有多个访问类 ...

  10. c++简单程序设计 实验一

    实验内容: 2-28 实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Selete one:”提示用户输入.A表示增加,D表示删除, S表示排序 ...