2010年11月8日 星期一

ASP.NET 線上產生XML不存在Server上

'*************************************************************
' 前端
'*************************************************************
需有以下的設定,此標籤必須原來就存在,無法動態產生此標籤再來下載。
解法:先有此標籤,href屬性動態指定即可。
$('#outputxml').attr("href",'OutputUD.ashx?selsi=' + si + '&selsn=' + SelSub);


'*************************************************************
' 後端
'*************************************************************
OutputUD.ashx

990125 防止其它網域的使用者呼叫!
If context.Request.UrlReferrer.Host <> context.Request.ServerVariables("SERVER_NAME") Then
FormsAuthentication.SignOut() '981208
context.Response.Redirect("ShowUserRdr.aspx")
End If

context.Response.ClearHeaders()
context.Response.Clear()
context.Response.Expires = 0
context.Response.Buffer = True
context.Response.AddHeader("Accept-Language", "zh-tw")

'檔案名稱
Dim strContentDisposition As String = "attachment; filename=UserRecord.xml"
context.Response.AddHeader("Content-Disposition", strContentDisposition)
context.Response.ContentType = "Application/octet-stream"

'檔案內容
Dim settings As New XmlWriterSettings
settings.Indent = True
settings.OmitXmlDeclaration = False
settings.NewLineOnAttributes = True
settings.Encoding = Encoding.UTF8

'將XML寫入的結果放在 context.Response.OutputStream內
Dim MyWriter As XmlWriter = XmlWriter.Create(context.Response.OutputStream, settings)

Dim sb As New StringBuilder
Dim i As Integer
'*********************************************************************************************** ' 990115 XML資料架構
' 1. UserRdr (root)
' |
' ----- SelectQ
' :
' ---------1. QASubject (選擇題目) 其下標籤
' :
' --------2. QAChinese (申論題目) 其下標籤
' :
' --------3. Customize (自訂或重點資料列表) 其下標籤
' :
' --------4. LawInfo (法規命令) 其下標籤
' :
' --------5. PointRecover (選擇題複習重點) 其下標籤
' :
' :------- 0. Finfo 關於檔案之資訊
'*********************************************************************************************** MyWriter.WriteStartElement("UserRdr")
MyWriter.WriteStartElement("SelectQ")

....................
'990104 考題出處
'MyWriter.WriteElementString("QFrom", "")
MyWriter.WriteStartElement("QFrom")
MyWriter.WriteCData(dr("QSource"))
MyWriter.WriteEndElement()


'990104 科目(僅抓取第一個編號 避免 x6x33x 這種多編碼形式!)
'MyWriter.WriteElementString("Qsub", "")
MyWriter.WriteStartElement("Qsub")
MyWriter.WriteCData(OutSubName)
MyWriter.WriteEndElement()
End While
MyWriter.WriteEndElement()

'990120 最後輸出 檔案的相關資訊!
MyWriter.WriteStartElement("Finfo") '< S >
MyWriter.WriteElementString("WDate", Now)
MyWriter.WriteElementString("S1", "")
MyWriter.WriteElementString("S2", "")
MyWriter.WriteElementString("S3", "")
MyWriter.WriteElementString("S4", "")
MyWriter.WriteEndElement()



'輸出結束標記!
MyWriter.WriteEndElement()
MyWriter.WriteEndElement()
'將XML輸出
MyWriter.Flush()

'將 context.Response.OutputStream 內容輸出
context.Response.OutputStream.ToString()

context.Response.End()


'判斷是否空字串 DBNull 或 特殊字元 ^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF
Function ReplaceSpChr(ByVal k As String) As String
'Return Regex.Replace(k, "[^\x09\x0A\x0D\x20-\xD7FF\xE000-\xFFFD\x10000-x10FFFF]", "", RegexOptions.IgnoreCase)
Return Regex.Replace(k, "[\f]", "", RegexOptions.IgnoreCase)
End Function

沒有留言:

張貼留言

追蹤者