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. js获取HTML DOM节点元素方法总结

    1. 通过顶层document节点获取:    (1)document.getElementById(elementId) :通过ID获得节点,如果页面上含有多个相同id的节点,那么只返回第一个节点. ...

  2. 洛谷P4778 Counting swaps 数论

    正解:数论 解题报告: 传送门! 首先考虑最终的状态是固定的,所以可以知道初始状态的每个数要去哪个地方,就可以考虑给每个数$a$连一条边,指向一个数$b$,表示$a$最后要移至$b$所在的位置 显然每 ...

  3. 博客搬家 https://hanwang945.github.io/

    博客搬家 https://hanwang945.github.io/

  4. wrapper induction随笔

    本文是一篇介绍Wrapper Induction的阅读笔记,原文详见<Wrapper induction:Efficiency and expressiveness>. Wrapper I ...

  5. 记录请求的耗时(拦截器、过滤器、aspect)

    文章前言 记录控制器请求的耗时处理通常有三种实现方式,分别是:过滤器.拦截器.aspect:下文将逐一实现. 1.Filter 过滤器 1.1.方法说明 需要实现 Filter 类,主要涉及三个方法: ...

  6. 移动端适配--flexible.js

    引言: H5适配一直是一个比较普遍的问题,很多文章都会讲,最近开发了一个H5的项目使用了一下淘宝的 flexible.js,写一篇文章自己总结一下. 一.背景介绍: Flexible.js是淘宝公开的 ...

  7. Synchronize,Lock, ReentrantLock,ReentrantReadWriteLock

    下面ReentrantLock是Lock接口的具体实现 如果想在线程等待时(即等待获得锁时)可以被中断,用lockInterruptibly:注意:在线程已经获得锁正在执行时,用Synchronize ...

  8. jQuery 选择具有特殊属性的元素

    如今有这样一种需求,须要选出全部有背景图片的元素. 这个问题有点棘手.我们无法使用选择表达式来完毕这个问题了. 使用jQuery的DOM过滤方法filter(),能够依据函数中表达的不论什么条件选择元 ...

  9. Github上36893颗星!这个被称为下一代企业级应用首选技术你学了么?

    ​ 用一句话概括:这个技术,是JAVA后端框架的龙头老大,执牛耳者.这个技术就是: Spring Boot春靴. Spring Boot到底凭什么成为Java社区最具影响力的项目?说直白点,他爹Spr ...

  10. Linux系统常用升级的基础包

    Linux系统常用升级的基础包 yum -y install lrzsz gcc gcc-c++ make flex autoconf automake vixie-cron libjpeg libj ...