标签:ase rect enc dom sel 图片路径 dir adt tostring
@{
ViewBag.Title = "知识上传";
}
@model Dictionary<string, bool>
//表单验证必填项目不为空函数
$(function () {
if ("@Model["upload"]" == "True") {
$(‘body‘).dailog({ type: ‘primary‘, title: ‘提示.‘, discription: ‘上传成功!‘ });
}
else if ("@Model["NoExistence"]" == "True") {
$(‘body‘).dailog({ type: ‘danger‘, title: ‘提示.‘, discription: ‘暂不支持该格式,请转换格式后上传!‘ });
}
else if ("@Model["FileTooLarge"]" == "True") {
$(‘body‘).dailog({ type: ‘danger‘, title: ‘提示.‘, discription: ‘文件过大,最大上限为400MB!‘ });
}
})
/// <summary>
/// 知识上传
/// </summary>
/// <returns></returns>
// GET: JKnowContent
public ActionResult Index()
{
var ret = new Dictionary<string, bool>();
ret.Add("upload", false);
ret.Add("FileTooLarge", false);
ret.Add("NoExistence", false);
return View(ret);
}
[HttpPost]
public ActionResult UploadFile(HttpPostedFileBase file1, string Pid, string FName)
{
try
{
TI_BUSER user = Session["LoginUser"] as TI_BUSER;
string UserOrgCode = user.ORGCODE;//当前登录用户的组织编码
// string UserOrgCode = "00542001001";
string OrgCode = UserOrgCode.Substring(0, 5);//截取用户组织编码的前5位为组织的编码
TI_BORGANIZE SchoolOrg = db.TI_BORGANIZE.Single(a => a.CODE == OrgCode);
string GetHtml = "";
var ret = new Dictionary<string, bool>();
//视频格式
string[] AllowedFileMove = new string[] { ".mp4", ".avi", ".rmvb" };
//文档格式
string[] AllowedFileExtensions = new string[] { ".docx", ".doc", ".png", ".jpg" };
//文件最大内存 50M
int MaxContentLength = 512000;
//知识标题
string KnowName = Request["FName"];
//图片路径
string ImgUrl = Request["SupplierSel"];
//根据ID 查询出详情信息
var eventlist = db.TB_KNOWLEDGECLASS.Single(a => a.STATUS == "1" && a.ID == Pid);
//文档类型
string EvenType = eventlist.FILETYPE;
if (EvenType == "视频")
{
if (!AllowedFileMove.Contains(file1.FileName.Substring(file1.FileName.LastIndexOf(‘.‘))))
{
ret.Add("upload", false);
ret.Add("FileTooLarge", false);
ret.Add("NoExistence", true);
return View("Index", ret);
}
else if (file1.ContentLength > MaxContentLength)
{
ret = new Dictionary<string, bool>();
ret.Add("upload", false);
ret.Add("NoExistence", false);
ret.Add("FileTooLarge", true);
return View("Index", ret);
}
}
else
{
if (!AllowedFileExtensions.Contains(file1.FileName.Substring(file1.FileName.LastIndexOf(‘.‘))))
{
ret.Add("upload", false);
ret.Add("FileTooLarge", false);
ret.Add("NoExistence", true);
return View("Index", ret);
}
else if (file1.ContentLength > MaxContentLength)
{
ret.Add("upload", false);
ret.Add("NoExistence", false);
ret.Add("FileTooLarge", true);
return View("Index", ret);
}
}
var fileName = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + file1.FileName;
var filePath = Server.MapPath("../Content/File");
file1.SaveAs(Path.Combine(filePath, fileName));
string ConD = "../Content/File/" + fileName.ToString();
string XpType = file1.FileName.Substring(file1.FileName.LastIndexOf(‘.‘));
string wordfile = filePath + "\\" + fileName;
if (XpType == ".doc" || XpType == ".docx")
{
Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
Type wordType = word.GetType();
Microsoft.Office.Interop.Word.Documents docs = word.Documents;
// 打开文件
Type docsType = docs.GetType();
object GetfileName = wordfile;
Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { GetfileName, true, true });
// 转换格式,另存为html
Type docType = doc.GetType();
//给文件重新起名
string GetfileName1 = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +
System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString();
string strFileFolder = "../Upload/html/";
DateTime dt = DateTime.Now;
//以yyyymmdd形式生成子文件夹名
string strFilePath = strFileFolder + "/";
// 判断指定目录下是否存在文件夹,如果不存在,则创建
if (!Directory.Exists(Server.MapPath(strFilePath)))
{
// 创建up文件夹
Directory.CreateDirectory(Server.MapPath(strFilePath));
}
//被转换的html文档保存的位置
// HttpContext.Current.Server.MapPath("html" + strFileSubFolder + GetfileName + ".html")
string ConfigPath = Server.MapPath(strFilePath + GetfileName1 + ".html");
object saveFileName = ConfigPath;
/*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:
* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML});
* 其它格式:
* wdFormatHTML
* wdFormatDocument
* wdFormatDOSText
* wdFormatDOSTextLineBreaks
* wdFormatEncodedText
* wdFormatRTF
* wdFormatTemplate
* wdFormatText
* wdFormatTextLineBreaks
* wdFormatUnicodeText
*/
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
//docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
// null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
//关闭文档
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { null, null, null });
// 退出 Word
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
System.IO.StreamReader sr = new System.IO.StreamReader(ConfigPath, Encoding.GetEncoding(0));
string html = sr.ReadToEnd();
sr.Close();
html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content=‘text/html; charset=gb2312‘>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
System.IO.StreamWriter sw = new System.IO.StreamWriter(ConfigPath, false, Encoding.Default);
sw.Write(html);
sw.Close();
GetHtml = html.Replace(GetfileName1, Upload.DGDomainUrl + "/Upload/html/" + GetfileName1);
}
if (XpType == ".doc" || XpType == ".docx")
{
byte[] byteArray = System.Text.Encoding.Default.GetBytes(GetHtml);
_KnoWled.COVERPHOTO = byteArray; //路径
}
else
{
byte[] byteArray = System.Text.Encoding.Default.GetBytes(ConD);
_KnoWled.COVERPHOTO = byteArray; //路径
}
ret.Add("upload", true);
ret.Add("NoExistence", false);
ret.Add("FileTooLarge", false);
return View("Index", ret);
}
catch (Exception Ery)
{
throw;
}
}
富文本编辑器内容生成Word 与 Dictionary的使用
标签:ase rect enc dom sel 图片路径 dir adt tostring
原文地址:http://www.cnblogs.com/shiwudefeng/p/7809858.html