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. [ipsec][crypto] 在IPSec ESP使用AES-GCM加密时的IV

    IV IV是指初始化向量. 在我们当前讨论的场景中: 在IPSec ESP使用AES-GCM加密 IV有两个含义: 1. ESP报文封装时的IV,RFC中称为 AES-GCM IV +-+-+-+-+ ...

  2. Ansoftmaxwell15.0

    电场磁场仿真软件安装出现问题: 基本问题都一样: 解决方式1:安装路径不要有中文的路径. 若安装提示vc++2005x86 安装失败 问题是:没有安装vc++2005 请安装vc++2005 x86 ...

  3. STP(Spanning Tree Protocol)

    STP生成树协议   问题 为了提高网络的可用性,需要进行冗余和备份.但是冗余路径会产生环路 环路会导致以下问题 广播风暴:由于交换机会对广播.多播.和未知目标MAC的单播包进行泛洪,在存在环路的情况 ...

  4. 基于token与基于服务器的身份认证

    1.基于服务器的身份认证 我们清楚 http 协议是无状态的,也就是说,如果我们已经认证了一个用户,那么他下一次请求的时候,服务器不知道我是谁,我们就必须要再次认证. 我们与浏览器交互时,比如说登陆成 ...

  5. Eclipse 创建第一个 springboot 应用

    1.前言 一直想把笔记整理出来,分享一下 springboot 的搭建: 因为私下 idea 用的比较多,使用比较方便,但恰逢小伙伴问起 eclipse 怎么搭建的问题, 顾整理以记之. 2.spri ...

  6. python 使用 PIL 和 matplotlib 获取图片像素点处理之后再写入

    python 版本 3.x 首先安装 PIL 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又 ...

  7. (转载)MyBatis传入多个参数的问题

    原文地址:https://www.cnblogs.com/mingyue1818/p/3714162.html 一.单个参数: public List<XXBean> getXXBeanL ...

  8. pytorch打印模型结构图

    import torchsummary from torchvision.models.resnet import * net = resnet18().cuda() print(net) 打印出来的 ...

  9. href和src的区别(小计)

    1.Src 是指向物件的来源地址,请求src资源时会将其指向的资源下载并应用文档中 src的内容是页面上比不可少的一部分,是引入.在 img.script.iframe 等元素上使用. 2.href ...

  10. PHP filter_var 函数用法

    先介绍下PHP Filter PHP手册地址:http://php.net/manual/zh/ref.filter.php PHP 过滤器用于对来自非安全来源的数据(比如用户输入)进行验证和过滤. ...