ImportFileHandler 附件上传
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 附件上传的更多相关文章
- asp.net结合uploadify实现多附件上传
1.说明 uploadify是一款优秀jQuery插件,主要功能是批量上传文件.大多数同学对多附件上传感到棘手,现将asp.net结合uploadfiy如何实现批量上传附件给大家讲解一下,有什么不对的 ...
- ueditor调用其中的附件上传功能
ueditor实际上是集成了webuploader, 在做内容发布的时候想既有ueditor又有单独的附件上传按钮,这时再加载一个webuploader就显得过于臃肿了,单独利用ueditor的上传功 ...
- 基于MVC4+EasyUI的Web开发框架形成之旅--附件上传组件uploadify的使用
大概一年前,我还在用Asp.NET开发一些行业管理系统的时候,就曾经使用这个组件作为文件的上传操作,在随笔<Web开发中的文件上传组件uploadify的使用>中可以看到,Asp.NET中 ...
- 百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程之更改图片和附件上传路径
本文是接上一篇博客,如果有疑问请先阅读上一篇:百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程 默认UEditor上传图片的路径是,编辑器包目录里面的net目录下 下面就演示如 ...
- tp中附件上传文件,表单提交
public function tianjia(){ $goods=D('Goods'); if(!empty($_POST)){ if($_FILES['f_goods_image']['error ...
- 使用plupload做一个类似qq邮箱附件上传的效果
公司项目中使用的框架是springmvc+hibernate+spring,目前需要做一个类似qq邮箱附件上传的功能,暂时只是上传小类型的附件 处理过程和解决方案都需要添加附件,处理过程和解决方案都可 ...
- Dynamic CRM 2013学习笔记(十三)附件上传 / 上传附件
上传附件可能是CRM里比较常用的一个需求了,本文将介绍如何在CRM里实现附件的上传.显示及下载.包括以下几个步骤: 附件上传的web页面 附件显示及下载的附件实体 调用上传web页面的JS文件 实体上 ...
- playframework中多附件上传注意事项
playframework中多附件上传注意事项 2013年09月24日 play 暂无评论 //play版本问题 经确认,1.0.3.2版本下控制器中方法参数 List<File> fi ...
- JS实现多附件上传(asp.net)
前几天,用户提出一个需求-多附件上传,另外,每个上传文件要加一个别名,本人创新少,从网上收集了资料,稍微改写,满足了 客户的需求.在应用到程序之前,先做了个小测试,测试通过,小高兴,就记录下了这个小测 ...
随机推荐
- WSL(Windows Subsystem for Linux)笔记一安装与使用
1.安装linux子系统 很简单直接在启动或关闭windows功能 中选择“适用于linux的windows子系统”,确定安装后重启即可,安装还是比较快的只用了几分钟. 也可以直接使用shell命令行 ...
- Mybatis+Mysql逆向工程
目录结构: pom文件: <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...
- echo 与 printf的区别与联系
echo命令默认是带有换行符的. 如果想让echo命令去掉每一行后面的换行符 方法1; 如果确信自己的脚本程序只运行在bash上,可以使用如下语法来出去空格: echo -n "Is it ...
- 玩转Vuejs--数组监听
Vue中对数据的监听主要是依靠Object.defineProperty来实现的,这种实现主要是针对key/value形式的对象,对数组中值的变化是无能为力的,那么该如何对数组中的数据进行监听呢,下面 ...
- ADB——keyevent命令
基本格式 adb shell input keyevent xxx # xxx为具体操作对应的数字 keycode 官方 KEYCODE 链接:戳这里 0 KEYCODE_UNKNOWN 未知按键 1 ...
- ROS学习备忘
1. remap的解释 For example, you are given a node that says it subscribes to the "chatter" top ...
- Oracle 12C执行root.sh CLSRSC-119 CRS-8503 CLSRSC-366
1.环境 OS:SUSE Linux Enterprise 12 SP3 X86_64 (Kernel: 4.4.162-94.69.2) DB:12.2.0.1 两节点RAC 参考文档:在 SLES ...
- Google搜索中的突变XSS-JavaScript Library Introduced XSS Flaw in Google Search
前言2018年9月26日,开源Closure库(最初由谷歌创建并用于谷歌搜索)的一名开发人员创建了一个提交,删除了部分输入过滤.据推测,这是因为开发人员在用户界面设计方面出现了问题.但此次提交的开发人 ...
- MongoDB系列----mongostat
mongostat是mongodb自带的监测工具,位于bin目录下.能用于实时监测mongodb的运行状态.在mongodb运行出现问题需要检测的时候应该优先考虑使用mongostat查看mongo运 ...
- IntelliJ IDEA安装scala插件并创建scala示例
1.http://blog.csdn.net/a2011480169/article/details/52712421 2.http://blog.csdn.net/stark_summer/arti ...