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. springboot+rediscluster

    @EnableCaching @Configuration public class RedisConfiguration extends CachingConfigurerSupport { @Au ...

  2. Spring AOP功能和目标

    1.AOP的作用 在OOP中,正是这种分散在各处且与对象核心功能无关的代码(横切代码)的存在,使得模块复用难度增加.AOP则将封装好的对象剖开,找出其中对多个对象产生影响的公共行为,并将其封装为一个可 ...

  3. Python_tkinter(2)_常用控件

    1.Label--标签(文字/位图)控件 from tkinter import * root = Tk() root.geometry('200x200') # Label控件 字体.边框.背景 l ...

  4. DDoS攻击及防御措施

    SYN flood 伪造大量的源IP地址,分别向服务器端发送大量的SYN包,此时服务器端会返回SYN/ACK 包,因为伪造的IP并不会答应,服务器端并没有收到伪造的IP的回应,会重试3~5次并等待一个 ...

  5. [vue开发记录]vue仿ios原生datepicker实现

    先上个效果图 现在只开发了年月,还在优化. 在网上看了一个纯原生js实现实现惯性滚动和回弹的文章  地址:https://www.cnblogs.com/ranyonsue/p/8119155.htm ...

  6. Modelsimobjects空白无显示问题和ISim仿真时出现空白

    困扰朕长达一周的问题得以解决!!!!! 发生这种情况的根源是win10自带的防火墙的问题.只有关闭防火墙,再重新打开软件进行仿真就能出现正常的仿真界面. 关闭防火墙的方法为:控制面板>>系 ...

  7. CentOS 7.5 安装 Python3.7

    1.安装开发者工具 yum -y groupinstall "Development Tools" 2.安装Python编译依赖包 yum -y install openssl-d ...

  8. Linux资源分析工具杂谈(长文慎入)

    Linux资源分析工具杂谈 开篇之前请大家先思考一个问题:        磁盘的平均I/O响应时间是1 ms,这个指标是好,还是差? 众所周知,计算机科学是客观的,也就是说对于一个给定的问题,我们总是 ...

  9. 新增职责 不能从IE进入的问题 此责任无可用函数 (转)

    此责任无可用函数(The Function Is Not Available Under The Responsibility) When attempting to navigate to a fu ...

  10. 网络-05-端口号-F5-负载均衡设-linux端口详解大全--TCP注册端口号大全备

    [root@test1:Standby] config # [root@test1:Standby] config # [root@test1:Standby] config # [root@test ...