1. 程式人生 > >富文本編輯器內容生成Word 與 Dictionary的使用

富文本編輯器內容生成Word 與 Dictionary的使用

del exp for second line filename ted fig 文件夾

@{
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的使用