上传:

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文件 存入数据库 实战的更多相关文章

  1. c# word excel 二进制 存入数据库

    在Sql Server中存储.读写Word文件,需要将指定表字段添加为Image类型,示例表结构为:1 CREATE TABLE CONTRACTS ( 2 ID VARCHAR (50), 3 CO ...

  2. 运用PDO存储将图片、音频文件存入数据库

    在数据库中创建表格的时候,有一个字段为image,用来保存图片,那么其类型就是blob,关于blob,百度百科是这样描述的 BLOB (binary large object),二进制大对象,是一个可 ...

  3. 利用kettle组件导入excel文件到数据库

    利用kettle组件导入excel文件到数据库 1.     实现目标 把excel文件内容导入到目标表中:然后用java调用kettle的转换.excel文件的内容仅仅有两列,示比例如以下: wat ...

  4. 如何保存PDF、Word和Excel文件到数据库中

    在项目中,有时候我们很需要把PDF.Word和Excel文档等等上传到数据库,以便日后使用.今天这篇文章向大家讲解如何将这些文件保存到数据库的. 详细步骤 第一步:打开数据库,单击新建查询,创建一个名 ...

  5. MVC4.0 上传Excel并存入数据库

    这里的这个功能实现在WebForm很好实现,上传阶段简单的一个FileUoLoad控件就搞定了,什么取值,什么上传都是浮云,微软都帮我们封装好了,我们只需要一拖一拽就OK了,但这些在MVC中是不行的! ...

  6. PHP - 读取EXCEL内容 存入数据库

    <?php //设置请求头 header("Content-Type:text/html;charset=utf8"); header("Access-Contro ...

  7. Java 导入Excel文件到数据库

    原文:http://www.jb51.net/article/44021.htm 项目中要求读取excel文件内容,并将其转化为xml格式.常见读取excel文档一般使用POI和JExcelAPI这两 ...

  8. Apache POI 一键上传(导入excel文件到数据库)

    import cn.XXXX.bos.utils.PinYin4jUtils; import org.apache.commons.lang3.StringUtils; // HSSF:操作07版本之 ...

  9. 将文件夹下的所有csv文件存入数据库

    # 股票的多因子分层回测代码实现 import os import pymysql # import datetime, time # from config import * database_ta ...

随机推荐

  1. 【Redis】redis+php处理高并发,很好的教程||附上 php的文件锁

    链接至:http://blog.csdn.net/nuli888/article/details/51865401 很好的教程,其中redis+php有点小问题. 附上php文件锁: $fp = fo ...

  2. 【转载】正则过滤所有html标签,只留文字的方法。

    public static string Html2Text(string htmlStr) { if (String.IsNullOrEmpty(htmlStr)) { return "& ...

  3. iOS 获得通讯录中联系人的所有属性--b

    ABAddressBookRef addressBook = ABAddressBookCreate(); CFArrayRef results = ABAddressBookCopyArrayOfA ...

  4. C# params传递多个参数

    C#开发语言中 params 是关键字,可以指定在参数数目可变处采用参数的方法参数.在函数的参 数数目可变而执行的代码差异很小的时候很有用! params关键字表示函数的参数是可变个数的,即可变的方法 ...

  5. Django URL中r的意思

    例如: urlpatterns = patterns('', # ... (r'^time/plus/\d+/$', hours_ahead), # ... ) 正则表达式字符串的开头字母“r”. 它 ...

  6. 利用docker-maven-plugin快速交测

    目的 由开发环境交测的时候,通过docker镜像简化环境搭建及项目部署的过程. 环境描述 项目开发环境: windowns7 在windowns7中通过VMware Workstation安装Cent ...

  7. 字符串函数---itoa()函数具体解释及实现

    itoa()函数 itoa():char *itoa( int value, char *string,int radix); 原型说明: value:欲转换的数据. string:目标字符串的地址. ...

  8. IronPython使用

    C#: class Program { static void Main(string[] args) { ScriptEngine engine = Python.CreateEngine(); S ...

  9. shell判断文件夹是否存在

    #shell判断文件夹是否存在 #如果文件夹不存在,创建文件夹 if [ ! -d "/myfolder" ]; then mkdir /myfolder fi #shell判断文 ...

  10. vue-router介绍

    vue-router学习 转自:https://my.oschina.net/u/1416844/blog/849971 1. vue-router介绍 vue-router把react-router ...