protected void Page_Load(object sender, EventArgs e) { if (Session["username"] != null) { delete.Visible = true; } objConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr"].ToString(); objConnection.Open(); //word转html String Sq1 = "Select path From announcement Where title = '" + (String)Session["name"] + "'"; SqlCommand cmd = new SqlCommand(Sq1, objConnection); String mm = (String)cmd.ExecuteScalar(); Button btn = sender as Button; Office2HtmlHelper.Word2Html(mm, MapPath("/announcement/"), "max"); String Sq = "Select title From announcement Where title = '" + (String)Session["name"] + "'"; SqlCommand cmd1 = new SqlCommand(Sq, objConnection); String ming = (String)cmd1.ExecuteScalar(); Label4.Text = ming; objConnection.Close(); }
public ActionResult UploadFile() { Result <string> check = new Result <string>(); try { var files = System.Web.HttpContext.Current.Request.Files; if (files.Count == 0) { check.msg = "请选择要上传的图片!"; check.success = false; return(Json(check)); } //var imgpath = System.Web.HttpContext.Current.Request["path"].ToString(); string HtmlName = DateTime.Now.ToString("yyyyMMddHHmmss"); var imgpath = DateTime.Now.ToString("yyyyMMdd") + "\\" + HtmlName; int number = 0; List <Stream> streams = new List <Stream>(); // Dictionary<string, Stream> filedic = new Dictionary<string, Stream>(); var path = AppDomain.CurrentDomain.BaseDirectory + "Images\\" + imgpath; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } for (int i = 0; i < files.Count; i++) { var fileName = path + "\\" + files[i].FileName; files[i].SaveAs(fileName); bool ok = System.IO.File.Exists(fileName.ToString()); if (ok) { number++; } } if (number.Equals(files.Count)) { check.msg = "上传成功!"; check.success = true; var re = Office2HtmlHelper.writeHtml(path, HtmlName); if (!string.IsNullOrWhiteSpace(re)) { // check.data = "http://autohtml.spydf.com\\" + "Images\\" + imgpath +"\\"+ re; check.data = "http://localhost:52314\\" + "Images\\" + imgpath + "\\" + re; } } else { check.msg = "失败!"; check.success = false; } return(Json(check)); } catch (Exception ex) { check.msg = ex.ToString(); check.success = false; return(Json(check)); } }
protected void Look(object source, DataListCommandEventArgs e) { int id = (int)DataList1.DataKeys[e.Item.ItemIndex]; string MyConn = "Data Source=(local);Initial Catalog=LC;Integrated Security=True"; SqlConnection conn = new SqlConnection(MyConn); conn.Open(); //打开数据库 //Console.WriteLine("数据库打开成功!"); //创建数据库命令 SqlCommand cmd = conn.CreateCommand(); //创建查询语句 cmd.CommandText = "SELECT path FROM book where id=" + id; string path = cmd.ExecuteScalar().ToString(); SqlCommand cmd1 = conn.CreateCommand(); //创建查询语句 cmd.CommandText = "SELECT bookname FROM book where id=" + id; string bookname = cmd.ExecuteScalar().ToString(); Office2HtmlHelper.Word2Html(MapPath(path), MapPath("/File/"), bookname); if (e.CommandName == "look") { Response.Redirect("~/File/" + bookname + ".html"); } if (e.CommandName == "down") { string fileName = bookname; //客户端保存的文件名 string filePath = Server.MapPath(path); //路径 FileInfo fileInfo = new FileInfo(filePath); Response.Clear(); Response.ClearContent(); Response.ClearHeaders(); Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName); Response.AddHeader("Content-Length", fileInfo.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding", "binary"); Response.ContentType = "application/octet-stream"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); Response.WriteFile(fileInfo.FullName); Response.Flush(); Response.End(); } }
protected void ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName == "delete") { int id = (int)DataList1.DataKeys[e.Item.ItemIndex]; string MyConn = "Data Source=(local);Initial Catalog=LC;Integrated Security=True"; SqlConnection MyConnection = new SqlConnection(MyConn); string MyDelete = "Delete from book where id=" + id; SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection); MyConnection.Open(); MyCommand.ExecuteNonQuery(); MyConnection.Close(); Response.AddHeader("Refresh", "0"); } if (e.CommandName == "look") { int id = (int)DataList1.DataKeys[e.Item.ItemIndex]; string MyConn = "Data Source=(local);Initial Catalog=LC;Integrated Security=True"; SqlConnection conn = new SqlConnection(MyConn); conn.Open(); //打开数据库 //Console.WriteLine("数据库打开成功!"); //创建数据库命令 SqlCommand cmd = conn.CreateCommand(); //创建查询语句 cmd.CommandText = "SELECT path FROM book where id=" + id; string path = cmd.ExecuteScalar().ToString(); SqlCommand cmd1 = conn.CreateCommand(); //创建查询语句 cmd.CommandText = "SELECT bookname FROM book where id=" + id; string bookname = cmd.ExecuteScalar().ToString(); conn.Close(); Office2HtmlHelper.Word2Html(MapPath(path), MapPath("/File/"), bookname); Response.Redirect("~/File/" + bookname + ".html"); } }
public void PreviewFile() { string returnMsg = string.Empty; int errorCode = 0; // 校验参数 string[] parametersRequired = { "fileHistoryId" }; if (!CheckParamsRequired(parametersRequired, out errorCode, out returnMsg)) { JsonResult <string> resultPra = new JsonResult <string> { Code = errorCode, Message = returnMsg, Rows = 0, Result = null }; GenerateJson(resultPra); return; } string actPath = string.Empty; string taskRootFolder = string.Empty; string taskFolderWithoutEmpNo = string.Empty; try { string fileHistoryId = context.Request["fileHistoryId"]; string previewFolder = ConfigurationManager.AppSettings["filePreviewFolder"].ToString(); // 根据 ID 获取到 fileHistory 对象 FileHistory fileHistory = new FileHistoryBLL().GetModel(fileHistoryId); DataTable dtPrjIdAndCategory = new FileCategoryBLL().GetProjectIdByFileHistoryId(fileHistoryId).Tables[0]; string projectId = Convert.ToString(dtPrjIdAndCategory.Rows[0]["PROJECTID"]); string category = Convert.ToString(dtPrjIdAndCategory.Rows[0]["category"]); string folderName = Convert.ToString(dtPrjIdAndCategory.Rows[0]["folderName"]); bool flag = new FileCategoryBLL().GetFilePathByProjectId(projectId, category, folderName, false, out actPath, out taskRootFolder, out taskFolderWithoutEmpNo, out errorCode); string physicalFileName = Path.Combine(actPath, fileHistory.FILENAME); if (File.Exists(physicalFileName)) { System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server; string previewHtmlFile = Path.Combine(previewFolder, fileHistory.ID + ".html"); LogHelper.WriteLine("previewHtmlFile Path: " + previewHtmlFile); if (!File.Exists(previewHtmlFile)) { string extName = Path.GetExtension(physicalFileName); switch (extName) { case ".doc": case ".docx": //case ".rtf": Office2HtmlHelper.Word2Html(physicalFileName, previewFolder, fileHistory.ID); break; case ".xls": case ".xlsx": Office2HtmlHelper.Excel2Html(physicalFileName, previewFolder, fileHistory.ID); break; default: return; } } List <PreviewResult> lstPre = new List <PreviewResult>(); if (File.Exists(previewHtmlFile)) { string htmlFile = string.Format("/FilePreview/{0}.html", fileHistory.ID); PreviewResult pre = new PreviewResult(); pre.previewUrl = htmlFile; lstPre.Add(pre); } else { errorCode = 1; returnMsg = "文件未能成功生成!"; } JsonResult <PreviewResult> resultPre = new JsonResult <PreviewResult> { Code = errorCode, Message = returnMsg, Rows = 0, Result = lstPre }; GenerateJson(resultPre); return; } errorCode = 6001; returnMsg = ErrorCode.GetCodeMessage(errorCode); JsonResult <string> result = new JsonResult <string> { Code = errorCode, Message = returnMsg, Rows = 0, Result = null }; GenerateJson(result); return; } catch (Exception ex) { errorCode = 1; LogHelper.WriteLine("预览生成出错:" + ex.Message + ex.StackTrace); } }