摘要:讀取HTML檔更改完後另存
HttpRequest.PhysicalApplicationPath 屬性
取得目前正在執行的伺服器應用程式的根目錄之實體檔案系統路徑
命名空間: System.Web
private string VersionHtml()
{
string m_return = "";
string m_tempFile = "SurveyTemp\\ResponsePage.htm"; //要讀取的htm
string m_inside_path = Request.PhysicalApplicationPath; //根目錄
string strPath = this.Server.MapPath("~/SurveyTemp/images/");
string strSaved_Path = "";
try
{
//第一個字元若為反斜線, 則先移除
if (m_tempFile.IndexOf('\\') == 0) m_tempFile = m_tempFile.Substring(1);
//檢查版型檔案是否存在(inside)
m_tempFile = m_inside_path + m_tempFile;
if (!System.IO.File.Exists(m_tempFile))
{
showPanelMsg("指定之版型檔案{0}已不存在!");
m_return = "";
}
string m_SurvePattern = System.IO.File.ReadAllText(m_tempFile, Encoding.Default); //讀取
string m_Textbgcolor = "background-color:" + xTextbgcolor.Text + ";";
string m_Textcolor = "color:" + xTextcolor.Text + ";";
string m_TextFontSize = "font-size:" + xcbTextFontSize.Value + ";";
string m_Text = "font-family:" + xcbText.Text + ";";
m_SurvePattern = m_SurvePattern.Replace("/*Text文字字體*/", m_Text);
m_SurvePattern = m_SurvePattern.Replace("/*Text文字顏色*/", m_Textcolor);
m_SurvePattern = m_SurvePattern.Replace("/*Text背景顏色*/", m_Textbgcolor);
m_SurvePattern = m_SurvePattern.Replace("/*Text文字大小*/", m_TextFontSize);
if (xcbTextWeight.Value == "b")
m_SurvePattern = m_SurvePattern.Replace("/*Text文字粗細*/", "font-weight:bold;");
if (xcbTextStyle.Value == "i")
m_SurvePattern = m_SurvePattern.Replace("/*Text文字樣式*/", "font-style:italic;");
m_SurvePattern = m_SurvePattern.Replace("<!--內容-->", xText.Text);
if (FileUp.HasFile) //Fileupload有檔案
{
if (CheckFiles("FileUp")) //檢查請參閱檢查Fileupload檔名
{
String fileExtension = System.IO.Path.GetExtension(FileUp.FileName).ToLower(); //取得檔案類型
string m_Response = String.Format("{0}{1}_{2}" + fileExtension
, "Response"
, DateTime.Today.ToString("yyyy_MM")
, DateTime.Now.Millisecond.ToString("0000"));
while (true)
{
if (!File.Exists(m_inside_path + fileExtension + m_Response)) break; //檢查有沒有檔案重複,有的話重新命名
m_Response = String.Format("{0}{1}_{2}" + fileExtension
, "Response"
, DateTime.Today.ToString("yyyy_MM")
, DateTime.Now.Millisecond.ToString("0000"));
}
//檔案放置路徑
strSaved_Path = Path.Combine(strPath, m_Response); //組合兩個路徑字串
FileUp.SaveAs(strSaved_Path);
//取得檔案名稱
string strFile_Name = "./images/" + m_Response;
string Responseing = " <img src=\"" + strFile_Name + "\" alt=\"\"/>";
m_SurvePattern = m_SurvePattern.Replace("<!--圖片-->", Responseing);
//Responseing字串 替代html裡的"<!--圖片-->"
}
else
{
showPanelMsg("圖檔錯誤!!");
m_return = "";
}
}
//產生版型html重新命名
m_return = String.Format("{0}_{1}.htm"
, DateTime.Now.ToString("yyyyMMdd_HHmm")
, DateTime.Now.Millisecond.ToString("0000"));
while (true)
{
if (!File.Exists(m_inside_path + @"SurveyTemp\" + m_return)) break;//檢查有沒有檔案重複,有的話重新命名
m_return = String.Format("{0}_{1}.htm"
, DateTime.Now.ToString("yyyyMMdd_HHmm")
, DateTime.Now.Millisecond.ToString("0000"));
}
StreamWriter m_sw = new StreamWriter(m_inside_path + @"SurveyTemp\" + m_return, false, Encoding.Default); //存檔
m_sw.Write(m_SurvePattern); //寫入
m_sw.Close();
}
catch (Exception ex)
{
m_return = "";
}
return m_return;
}
_________________________________________________________________________________________________
HTML頁面
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>感謝您的填寫!</title>
<style type="text/css">
.Textside
{
height: 20px;
/*Text背景顏色*/
/*Text文字顏色*/
/*Text文字字體*/
/*Text文字大小*/
/*Text文字粗細*/
/*Text文字樣式*/
}
</style>
</head>
<body>
<form action="" >
<table style="height:150px; width:100%;">
<tr>
<td colspan="3">
</td>
</tr>
<tr>
<td style="width:20%;" rowspan="2"></td>
<td style="width:60%;height:200px;" align="center">
<!--圖片-->
</td>
<td style="width:20%;" rowspan="2"></td>
</tr>
<tr>
<td style="width:60%;" align="center" class="Textside">
<!--內容-->
</td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
</table>
</form>
<!--感謝參與本活動!!-->
</body>
</html>