word excel文件 存入数据库 实战
上传:
private void Insert2017(HttpContext context)
{
if (context.Request.Files["fileword"].ContentLength > 0)
{ if (context.Request["OUId"] != null &&
context.Request["OUName"] != null &&
context.Request["PersonId"] != null &&
context.Request["PersonName"] != null &&
context.Request["DocType"] != null)
{
string OUId = context.Request["OUId"].ToString();
string OUCODE = context.Request["OUCODE"].ToString();
string OUName = context.Request["OUName"].ToString();
string PersonId = context.Request["PersonId"].ToString();
string PersonName = context.Request["PersonName"].ToString();
string DocType = context.Request["DocType"].ToString();
string Remarks = context.Request["Remarks"].ToString();
string FileName = Path.GetFileName(context.Request.Files["fileword"].FileName); string DownLoadUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName + CheckLoadUrl(DocType) + GetFileName(DocType, OUName, context, true); string LoadUrl = "";
string flagUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName; string nameflag = CheckFileName(FileName, DocType, OUName);
if (nameflag == "校验成功")
{
if (Directory.Exists(flagUrl + CheckLoadUrl(DocType)))
{
LoadUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName + CheckLoadUrl(DocType) + FileName;
}
else
{
Directory.CreateDirectory(flagUrl);
Directory.CreateDirectory(flagUrl + "\\常规2017");
LoadUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName + CheckLoadUrl(DocType) + FileName; }
try
{ //InsertData(OUCODE, OUId, OUName, PersonId, PersonName, FileName, LoadUrl, Remarks, DocType, DownLoadUrl);
var fileinfo = context.Request.Files["fileword"];
Stream inputStream = fileinfo.InputStream;
int contentLength = fileinfo.ContentLength;
byte[] content = new byte[contentLength];
inputStream.Read(content, 0, content.Length);
inputStream.Close();
InsertData(OUCODE, OUId, OUName, PersonId, PersonName, FileName, LoadUrl, Remarks, DocType, DownLoadUrl,contentLength,content); //context.Response.Write("{success:true, files:'上传成功'}");
if (File.Exists(LoadUrl))
{
File.Delete(LoadUrl);
LoadUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName + CheckLoadUrl(DocType) + GetFileName(DocType, OUName, context, true);
context.Request.Files["fileword"].SaveAs(LoadUrl);
context.Response.Write("{success:true, files:'文件替换成功'}");
}
else
{
LoadUrl = context.Server.MapPath("~/ProjectMgt/Doc2017") + "\\" + OUName + CheckLoadUrl(DocType) + GetFileName(DocType, OUName, context, true);
context.Request.Files["fileword"].SaveAs(LoadUrl);
context.Response.Write("{success:true, files:'上传成功'}");
} }
finally
{
context.Response.End();
}
}
else
{
context.Response.Write("{success:false, files:'" + nameflag + "'}");
context.Response.End();
}
}
else
{
context.Response.Write("{success:false, files:'上传失败'}");
context.Response.End();
} }
else
{
context.Response.Write("{success:false, files:'上传失败'}");
context.Response.End();
} }
private bool InsertData(string oucode, string ouid, string ouname,
string personid, string personname, string filename,
string loadurl, string remarks, string doctype,
string downloadurl,int contentLength, byte[] content)
{
ExtFacade ef = new ExtFacade(); Guid id = Guid.NewGuid();
string OUId = ouid;
string OUCODE = oucode;
string OUName = ouname;
string PersonId = personid;
string PersonName = personname;
string FileName = filename;
string LoadUrl = loadurl;
DateTime SubmitTime = DateTime.Now;
string Remarks = remarks;
string DocType = doctype;
string DownLoadUrl = downloadurl; byte[] Content = content;
int ContentLength = contentLength;
string DownLoadFileName = downloadurl.Substring(downloadurl.LastIndexOf("\\")+1);
string ContentType = GetContentType(DownLoadFileName);
//string query = @"INSERT INTO tbl_Net_SecurityCheck_2017 (
// id,
// OUID,
// OUName,
// PersonId,
// PersonName,
// FileName,
// LoadUrl,
// SubmitTime,
// Remarks,
// DocType,
// DownloadUrl,
// OUCODE,
// Content,
// ContentLength,
// DownLoadFileName,
// ContentType
// )
// VALUES
// (
// @id,
// @OUId,
// @OUName,
// @PersonId,
// @PersonName,
// @FileName,
// @LoadUrl,
// @SubmitTime,
// @Remarks,
// @DocType,
// @DownLoadUrl,
// @OUCODE,
// @Content,
// @ContentLength,
// @DownLoadFileName,
// @ContentType
// )";
string query = @"INSERT INTO tbl_Net_SecurityCheck_2017 (
id,
OUID,
OUName,
PersonId,
PersonName,
FileName,
LoadUrl,
SubmitTime,
Remarks,
DocType,
DownloadUrl,
OUCODE,
Content,
ContentLength,
DownLoadFileName,
ContentType
)
VALUES
(
@id,
@OUId,
@OUName,
@PersonId,
@PersonName,
@FileName,
@LoadUrl,
@SubmitTime,
@Remarks,
@DocType,
@DownLoadUrl,
@OUCODE,
@Content,
@ContentLength,
@DownLoadFileName,
@ContentType
)";
//ef.ExeBySQLFile();
//ef.ExeBySQLText(query); using (SqlConnection _Connection = new SqlConnection(AppConfig.ConnectionString))
{
_Connection.Open();
//SqlDALBase dalbase = new SqlDALBase(_Connection, _Connection.BeginTransaction());
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = _Connection;
cmd.CommandText = query;
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@id", id));
cmd.Parameters.Add(new SqlParameter("@OUId", OUId));
cmd.Parameters.Add(new SqlParameter("@OUName", OUName));
cmd.Parameters.Add(new SqlParameter("@PersonId", PersonId));
cmd.Parameters.Add(new SqlParameter("@PersonName", PersonName));
cmd.Parameters.Add(new SqlParameter("@FileName", FileName));
cmd.Parameters.Add(new SqlParameter("@LoadUrl", LoadUrl));
cmd.Parameters.Add(new SqlParameter("@SubmitTime", SubmitTime));
cmd.Parameters.Add(new SqlParameter("@Remarks", Remarks));
cmd.Parameters.Add(new SqlParameter("@DocType", DocType));
cmd.Parameters.Add(new SqlParameter("@DownLoadUrl", DownLoadUrl));
cmd.Parameters.Add(new SqlParameter("@OUCODE", OUCODE));
cmd.Parameters.Add(new SqlParameter("@Content", SqlDbType.Image)); cmd.Parameters.Add(new SqlParameter("@ContentLength", ContentLength));
cmd.Parameters.Add(new SqlParameter("@DownLoadFileName", DownLoadFileName));
cmd.Parameters.Add(new SqlParameter("@ContentType", ContentType));
cmd.Parameters["@Content"].Value = Content;
int i = cmd.ExecuteNonQuery();
//return cmd.ExecuteNonQuery() > 0 ? true : false;
//List<SqlParameter> parameters = new List<SqlParameter>();
//parameters.Add(new SqlParameter("@id", id));
//parameters.Add(new SqlParameter("@OUId", OUId));
//parameters.Add(new SqlParameter("@OUName", OUName));
//parameters.Add(new SqlParameter("@PersonId", PersonId));
//parameters.Add(new SqlParameter("@PersonName", PersonName));
//parameters.Add(new SqlParameter("@FileName", FileName));
//parameters.Add(new SqlParameter("@LoadUrl", LoadUrl));
//parameters.Add(new SqlParameter("@SubmitTime", SubmitTime));
//parameters.Add(new SqlParameter("@Remarks", Remarks));
//parameters.Add(new SqlParameter("@DocType", DocType));
//parameters.Add(new SqlParameter("@DownLoadUrl", DownLoadUrl));
//parameters.Add(new SqlParameter("@OUCODE", OUCODE));
//parameters.Add(new SqlParameter("@Content", SqlDbType.Image));
//parameters.Add(new SqlParameter("@ContentLength", ContentLength));
//parameters.Add(new SqlParameter("@DownLoadFileName", DownLoadFileName));
//parameters.Add(new SqlParameter("@ContentType", ContentType));
//parameters[12].Value = Content;
//_Connection.(query, parameters, CommandType.Text);
} } return true;
}
下载:
private void DownLoad2017(HttpContext context)
{
//客户端保存的文件名
if (context.Request["DocType"] != null && context.Request["OUName"] != null)
{
string fileName = GetFileName(context.Request["DocType"].ToString(), context.Request["OUName"].ToString(), context, false);
string filePath = context.Request["DownLoadUrl"].ToString();//路径
string fileId = context.Request["FileId"].ToString();//路径
//以字符流的形式下载文件
if (!string.IsNullOrEmpty(filePath))
{
try
{
//FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = GetContent(fileId); context.Response.ContentType = "application/octet-stream";
//通知浏览器下载文件而不是打开
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
context.Response.BinaryWrite(bytes);
context.Response.Flush();
context.Response.End();
}
catch
{
}
}
}
else {
}
}
private byte[] GetContent(string fileId)
{
ExtFacade ef = new ExtFacade(); string query = string.Format(@"SELECT Content from tbl_Net_SecurityCheck_2017 where id='{0}'", fileId); var content = ef.ExecuteScalarBySQLText(query);
if (content == null)
{
return new byte[0];
}
else
{
return (byte[])content;
}
}
word excel文件 存入数据库 实战的更多相关文章
- c# word excel 二进制 存入数据库
在Sql Server中存储.读写Word文件,需要将指定表字段添加为Image类型,示例表结构为:1 CREATE TABLE CONTRACTS ( 2 ID VARCHAR (50), 3 CO ...
- 运用PDO存储将图片、音频文件存入数据库
在数据库中创建表格的时候,有一个字段为image,用来保存图片,那么其类型就是blob,关于blob,百度百科是这样描述的 BLOB (binary large object),二进制大对象,是一个可 ...
- 利用kettle组件导入excel文件到数据库
利用kettle组件导入excel文件到数据库 1. 实现目标 把excel文件内容导入到目标表中:然后用java调用kettle的转换.excel文件的内容仅仅有两列,示比例如以下: wat ...
- 如何保存PDF、Word和Excel文件到数据库中
在项目中,有时候我们很需要把PDF.Word和Excel文档等等上传到数据库,以便日后使用.今天这篇文章向大家讲解如何将这些文件保存到数据库的. 详细步骤 第一步:打开数据库,单击新建查询,创建一个名 ...
- MVC4.0 上传Excel并存入数据库
这里的这个功能实现在WebForm很好实现,上传阶段简单的一个FileUoLoad控件就搞定了,什么取值,什么上传都是浮云,微软都帮我们封装好了,我们只需要一拖一拽就OK了,但这些在MVC中是不行的! ...
- PHP - 读取EXCEL内容 存入数据库
<?php //设置请求头 header("Content-Type:text/html;charset=utf8"); header("Access-Contro ...
- Java 导入Excel文件到数据库
原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...
- Apache POI 一键上传(导入excel文件到数据库)
import cn.XXXX.bos.utils.PinYin4jUtils; import org.apache.commons.lang3.StringUtils; // HSSF:操作07版本之 ...
- 将文件夹下的所有csv文件存入数据库
# 股票的多因子分层回测代码实现 import os import pymysql # import datetime, time # from config import * database_ta ...
随机推荐
- TFS创建登录用户并连接TFS
简单使用 1.创建TFS组(Windows的组) 2.在“TFS管理控制台”中设置“组成员资格” 3.给“Team Foundation Administrators”组设置“属性” 4.给“Team ...
- Postman+Postman interceptor的安装和使用-解决把chrome浏览器登录状态同步到postman进行有依赖的接口测试 Postman 使用方法详解
Postman+Postman interceptor的安装和使用-解决把chrome浏览器登录状态同步到postman进行有依赖的接口测试 问题引入:做接口测试时,有依赖关系的接口往往不好测试( ...
- CentOS Linux防火墙配置及关闭
CentOS 配置防火墙操作实例(启.停.开.闭端口): 注:防火墙的基本操作命令: 查询防火墙状态: [root@localhost ~]# service iptables status< ...
- java线程同步方法,方法块差别
先说同步方法.它究竟是锁定的当前对象,还是当前类 代码块1 package com.ssss; public class Thread1 implements Runnable { //public ...
- [Jobdu] 题目1139:最大子矩阵
题目描述: 已知矩阵的大小定义为矩阵中所有元素的和.给定一个矩阵,你的任务是找到最大的非空(大小至少是1 * 1)子矩阵.比如,如下4 * 4的矩阵 0 -2 -7 09 2 -6 2-4 1 -4 ...
- Android系统应用信息中存储和缓存的计算方法
进行例如以下操作: 设置->应用->选择一个应用->应用信息 会到达例如以下界面: 能够看到这个应用占用的磁盘空间. 先说结果,这几项会计算哪些文件(夹). 1.应用,由三项相加组成 ...
- alloc retain release函数
- SimpleDateFormat线程不安全及解决的方法
一. 为什么SimpleDateFormat不是线程安全的? Java源代码例如以下: /** * Date formats are not synchronized. * It is recomme ...
- sim900GPRS模块ppp拨号上网
--------------------------------------------- 主机操作系统:Centos 6.5 交叉编译器环境:arm-linux-gcc-4.5.4 开发板平台: F ...
- -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc830110
问题 今天做项目,遇到了这个问题 -[__NSArrayI removeAllObjects]: unrecognized selector sent to instance 0x7fa8dc8301 ...