.ics 文件的响应 .write 不允许代码 运行
Response .write for .ics file not allowing code to run
我正在编写一个应用程序,它通过获取用户输入的数据动态构建 .ICS 文件。我想要做的是将数据保存到数据库中,然后提供 .ICS 文件供用户下载,以便他们可以将其添加到他们的日历中。我希望所有这一切都可以通过单击一个按钮来实现。我 运行 遇到的问题是,当单击按钮时,它正在写入文件,因此用户可以下载它,但它不会将数据存储到数据库中。它停留在页面上并且不会出现回发。
Public Sub AddToCalendar(ByVal Src As Object, ByVal e As EventArgs)
Try
Dim Subject As String = setDBText(txtSubject.text)
Dim Comment As String = setDBText(txtComment.text)
Dim StartDate As String = setDBDate(txtStartDate.value,txtStartTime.text)
Dim EndDate As String = setDBDate(txtEndDate.value,txtEndTime.text)
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES(" & Subject & "," & Comment & "," & StartDate & "," & EndDate &")"
dc.CommandText = sqlStr
dc.ExecuteNonQuery()
lblTest.Text = "New Event Sucessfully Added"
'************
'** CREATE CODE TO BUILD ICS FILE
'************
Dim sbICSFile As StringBuilder = New StringBuilder()
Dim dtNow As DateTime = DateTime.Now
sbICSFile.AppendLine("BEGIN:VCALENDAR")
sbICSFile.AppendLine("VERSION:2.0")
sbICSFile.AppendLine("PRODID:-//AkonaDev/CalendarAppointment")
sbICSFile.AppendLine("CALSCALE:GREGORIAN")
sbICSFile.AppendLine("BEGIN:VEVENT")
' Define time zones.
' US/Eastern
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Eastern")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0400")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:EST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0400")
sbICSFile.AppendLine("TZNAME:EDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Central
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Central")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:CST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:CDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Mountain
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Mountain")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:MST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:MDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Pacific
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Pacific")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0800")
sbICSFile.AppendLine("TZNAME:PST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0800")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:PDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' Define the event
sbICSFile.Append("DTSTART;TZID=" + ddlStartTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year.ToString())
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlStartTime.SelectedValue)
sbICSFile.Append("DTEND;TZID=" + ddlEndTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year)
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlEndTime.SelectedValue)
sbICSFile.AppendLine("SUMMARY:" + txtEventSubject.Text)
sbICSFile.AppendLine("DESCRIPTION:" + txtEventDescription.Text)
sbICSFile.AppendLine("UID:1")
sbICSFile.AppendLine("SEQUENCE:0")
sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString())
sbICSFile.Append(FormatDateTimeValue(dtNow.Month))
sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T")
sbICSFile.Append(FormatDateTimeValue(dtNow.Hour))
sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00")
sbICSFile.AppendLine("END:VEVENT")
sbICSFile.AppendLine("END:VCALENDAR")
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", _
"attachment; filename=CalendarEvent1.ics")
Response.Write(sbICSFile)
Response.End()
Catch exc As Exception
lblError.text = exc.toString
End Try
我注意到的第一件事是您的 INSERT
声明似乎有一些问题。 (这可能就是代码块中的语法着色看起来不正确的原因。)字符串通常用单引号引起来。这应该会导致抛出异常,所以我不确定它是如何继续过去 ExecuteNonQuery
的。我希望看到更多这样的东西。
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES('" & Subject & "','" & Comment & "','" & StartDate & "','" & EndDate &"')"
更好的选择是参数化语句。请仔细阅读。您的代码原样容易受到 SQL 注入的攻击。 How does SQLParameter prevent SQL Injection?
我发现响应一次只能做其中一件事情。它不能发送文件和执行服务器代码。所以我所做的是创建一个单独的 "file download" 页面来处理 page_load 上的文件下载。为了将 ICS 文件数据传输到文件下载页面,我创建了一个会话变量,该变量在提交页面的按钮单击时被填充。然后,我向按钮添加了一个 onClientClick 事件,并将其设置为一个 javascript 函数,该函数会打开 window 到文件下载页面的弹出窗口。这允许页面将数据保存到数据库,并通过单击按钮显示文件下载提示。
提交页面
Public Sub AddToCalendar(ByVal Src As Object, ByVal e As EventArgs)
Try
Dim Subject As String = setDBText(txtSubject.text)
Dim Comment As String = setDBText(txtComment.text)
Dim StartDate As String = setDBDate(txtStartDate.text,txtStartTime.text)
Dim EndDate As String = setDBDate(txtEndDate.text,txtEndTime.text)
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES(" & Subject & "," & Comment & "," & StartDate & "," & EndDate &")"
dc.CommandText = sqlStr
dc.ExecuteNonQuery()
lblTest.Text = "New Event Sucessfully Added"
'************
'** CREATE CODE TO BUILD ICS FILE
'************
Dim sbICSFile As StringBuilder = New StringBuilder()
Dim dtNow As DateTime = DateTime.Now
sbICSFile.AppendLine("BEGIN:VCALENDAR")
sbICSFile.AppendLine("VERSION:2.0")
sbICSFile.AppendLine("PRODID:-//AkonaDev/CalendarAppointment")
sbICSFile.AppendLine("CALSCALE:GREGORIAN")
sbICSFile.AppendLine("BEGIN:VEVENT")
' Define time zones.
' US/Eastern
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Eastern")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0400")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:EST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0400")
sbICSFile.AppendLine("TZNAME:EDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Central
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Central")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:CST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:CDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Mountain
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Mountain")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:MST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:MDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Pacific
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Pacific")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0800")
sbICSFile.AppendLine("TZNAME:PST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0800")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:PDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' Define the event
sbICSFile.Append("DTSTART;TZID=" + ddlStartTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year.ToString())
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlStartTime.SelectedValue)
sbICSFile.Append("DTEND;TZID=" + ddlEndTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year)
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlEndTime.SelectedValue)
sbICSFile.AppendLine("SUMMARY:" + txtEventSubject.Text)
sbICSFile.AppendLine("DESCRIPTION:" + txtEventDescription.Text)
sbICSFile.AppendLine("UID:1")
sbICSFile.AppendLine("SEQUENCE:0")
sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString())
sbICSFile.Append(FormatDateTimeValue(dtNow.Month))
sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T")
sbICSFile.Append(FormatDateTimeValue(dtNow.Hour))
sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00")
sbICSFile.AppendLine("END:VEVENT")
sbICSFile.AppendLine("END:VCALENDAR")
'** SET TO SESSION VARIABLE INSTEAD OF CALLING RESPONSE.WRITE **
Session("ICSFile") = sbICSFile.ToString
Catch exc As Exception
lblError.text = exc.toString
End Try
End Sub
ICS 文件下载页面
Sub Page_Load(Src As Object, E As EventArgs)
Try
'** GET ICS FILE FROM SESSION VARIABLE
Dim ICSFile As String = Session("ICSFile").ToString
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", "attachment; filename=CalendarEvent1.ics")
Response.Write(ICSFile)
Response.End()
Catch exc As Exception
lblError.text = exc.toString
End Try
End Sub
HTML
asp:按钮 ID="btnAddToCalendar" runat="server" OnClick="AddToCalendar" OnClientClick="javascript:DownloadICS();" Text="Add To Calendar" />
JavaScript
<script type="text/javascript" language="javascript">
function DownloadICS() {
popUp("ics_download.aspx", 300, 300);
}
function popUp(url, width, height) {
if (window.featWin && !window.featWin.closed) {
window.featWin.close();
featWin = window.open(url, "win", 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
selfWin = self;
featWin.focus();
}
else {
featWin = window.open(url, "win", 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
selfWin = self;
featWin.focus();
}
}
function closeWin() {
if (window.featWin && !window.featWin.closed) {
window.featWin.close();
}
}
我正在编写一个应用程序,它通过获取用户输入的数据动态构建 .ICS 文件。我想要做的是将数据保存到数据库中,然后提供 .ICS 文件供用户下载,以便他们可以将其添加到他们的日历中。我希望所有这一切都可以通过单击一个按钮来实现。我 运行 遇到的问题是,当单击按钮时,它正在写入文件,因此用户可以下载它,但它不会将数据存储到数据库中。它停留在页面上并且不会出现回发。
Public Sub AddToCalendar(ByVal Src As Object, ByVal e As EventArgs)
Try
Dim Subject As String = setDBText(txtSubject.text)
Dim Comment As String = setDBText(txtComment.text)
Dim StartDate As String = setDBDate(txtStartDate.value,txtStartTime.text)
Dim EndDate As String = setDBDate(txtEndDate.value,txtEndTime.text)
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES(" & Subject & "," & Comment & "," & StartDate & "," & EndDate &")"
dc.CommandText = sqlStr
dc.ExecuteNonQuery()
lblTest.Text = "New Event Sucessfully Added"
'************
'** CREATE CODE TO BUILD ICS FILE
'************
Dim sbICSFile As StringBuilder = New StringBuilder()
Dim dtNow As DateTime = DateTime.Now
sbICSFile.AppendLine("BEGIN:VCALENDAR")
sbICSFile.AppendLine("VERSION:2.0")
sbICSFile.AppendLine("PRODID:-//AkonaDev/CalendarAppointment")
sbICSFile.AppendLine("CALSCALE:GREGORIAN")
sbICSFile.AppendLine("BEGIN:VEVENT")
' Define time zones.
' US/Eastern
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Eastern")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0400")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:EST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0400")
sbICSFile.AppendLine("TZNAME:EDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Central
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Central")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:CST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:CDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Mountain
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Mountain")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:MST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:MDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Pacific
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Pacific")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0800")
sbICSFile.AppendLine("TZNAME:PST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0800")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:PDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' Define the event
sbICSFile.Append("DTSTART;TZID=" + ddlStartTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year.ToString())
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlStartTime.SelectedValue)
sbICSFile.Append("DTEND;TZID=" + ddlEndTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year)
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlEndTime.SelectedValue)
sbICSFile.AppendLine("SUMMARY:" + txtEventSubject.Text)
sbICSFile.AppendLine("DESCRIPTION:" + txtEventDescription.Text)
sbICSFile.AppendLine("UID:1")
sbICSFile.AppendLine("SEQUENCE:0")
sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString())
sbICSFile.Append(FormatDateTimeValue(dtNow.Month))
sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T")
sbICSFile.Append(FormatDateTimeValue(dtNow.Hour))
sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00")
sbICSFile.AppendLine("END:VEVENT")
sbICSFile.AppendLine("END:VCALENDAR")
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", _
"attachment; filename=CalendarEvent1.ics")
Response.Write(sbICSFile)
Response.End()
Catch exc As Exception
lblError.text = exc.toString
End Try
我注意到的第一件事是您的 INSERT
声明似乎有一些问题。 (这可能就是代码块中的语法着色看起来不正确的原因。)字符串通常用单引号引起来。这应该会导致抛出异常,所以我不确定它是如何继续过去 ExecuteNonQuery
的。我希望看到更多这样的东西。
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES('" & Subject & "','" & Comment & "','" & StartDate & "','" & EndDate &"')"
更好的选择是参数化语句。请仔细阅读。您的代码原样容易受到 SQL 注入的攻击。 How does SQLParameter prevent SQL Injection?
我发现响应一次只能做其中一件事情。它不能发送文件和执行服务器代码。所以我所做的是创建一个单独的 "file download" 页面来处理 page_load 上的文件下载。为了将 ICS 文件数据传输到文件下载页面,我创建了一个会话变量,该变量在提交页面的按钮单击时被填充。然后,我向按钮添加了一个 onClientClick 事件,并将其设置为一个 javascript 函数,该函数会打开 window 到文件下载页面的弹出窗口。这允许页面将数据保存到数据库,并通过单击按钮显示文件下载提示。
提交页面
Public Sub AddToCalendar(ByVal Src As Object, ByVal e As EventArgs)
Try
Dim Subject As String = setDBText(txtSubject.text)
Dim Comment As String = setDBText(txtComment.text)
Dim StartDate As String = setDBDate(txtStartDate.text,txtStartTime.text)
Dim EndDate As String = setDBDate(txtEndDate.text,txtEndTime.text)
sqlStr = "INSERT INTO tblActivity(Subject,Comment,StartDate,EndDate) " & _
"VALUES(" & Subject & "," & Comment & "," & StartDate & "," & EndDate &")"
dc.CommandText = sqlStr
dc.ExecuteNonQuery()
lblTest.Text = "New Event Sucessfully Added"
'************
'** CREATE CODE TO BUILD ICS FILE
'************
Dim sbICSFile As StringBuilder = New StringBuilder()
Dim dtNow As DateTime = DateTime.Now
sbICSFile.AppendLine("BEGIN:VCALENDAR")
sbICSFile.AppendLine("VERSION:2.0")
sbICSFile.AppendLine("PRODID:-//AkonaDev/CalendarAppointment")
sbICSFile.AppendLine("CALSCALE:GREGORIAN")
sbICSFile.AppendLine("BEGIN:VEVENT")
' Define time zones.
' US/Eastern
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Eastern")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0400")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:EST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0400")
sbICSFile.AppendLine("TZNAME:EDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Central
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Central")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0500")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:CST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0500")
sbICSFile.AppendLine("TZNAME:CDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Mountain
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Mountain")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0600")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:MST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0600")
sbICSFile.AppendLine("TZNAME:MDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' US/Pacific
sbICSFile.AppendLine("BEGIN:VTIMEZONE")
sbICSFile.AppendLine("TZID:US/Pacific")
sbICSFile.AppendLine("BEGIN:STANDARD")
sbICSFile.AppendLine("DTSTART:20071104T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=1SU;BYMONTH=11")
sbICSFile.AppendLine("TZOFFSETFROM:-0700")
sbICSFile.AppendLine("TZOFFSETTO:-0800")
sbICSFile.AppendLine("TZNAME:PST")
sbICSFile.AppendLine("END:STANDARD")
sbICSFile.AppendLine("BEGIN:DAYLIGHT")
sbICSFile.AppendLine("DTSTART:20070311T020000")
sbICSFile.AppendLine("RRULE:FREQ=YEARLY;BYDAY=2SU;BYMONTH=3")
sbICSFile.AppendLine("TZOFFSETFROM:-0800")
sbICSFile.AppendLine("TZOFFSETTO:-0700")
sbICSFile.AppendLine("TZNAME:PDT")
sbICSFile.AppendLine("END:DAYLIGHT")
sbICSFile.AppendLine("END:VTIMEZONE")
' Define the event
sbICSFile.Append("DTSTART;TZID=" + ddlStartTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year.ToString())
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlStartTime.SelectedValue)
sbICSFile.Append("DTEND;TZID=" + ddlEndTZ.Text + ":")
sbICSFile.Append(CDATE(txtStartDate.value).Year)
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Month))
sbICSFile.Append( _
FormatDateTimeValue(CDATE(txtStartDate.value).Day) + "T")
sbICSFile.AppendLine(ddlEndTime.SelectedValue)
sbICSFile.AppendLine("SUMMARY:" + txtEventSubject.Text)
sbICSFile.AppendLine("DESCRIPTION:" + txtEventDescription.Text)
sbICSFile.AppendLine("UID:1")
sbICSFile.AppendLine("SEQUENCE:0")
sbICSFile.Append("DTSTAMP:" + dtNow.Year.ToString())
sbICSFile.Append(FormatDateTimeValue(dtNow.Month))
sbICSFile.Append(FormatDateTimeValue(dtNow.Day) + "T")
sbICSFile.Append(FormatDateTimeValue(dtNow.Hour))
sbICSFile.AppendLine(FormatDateTimeValue(dtNow.Minute) + "00")
sbICSFile.AppendLine("END:VEVENT")
sbICSFile.AppendLine("END:VCALENDAR")
'** SET TO SESSION VARIABLE INSTEAD OF CALLING RESPONSE.WRITE **
Session("ICSFile") = sbICSFile.ToString
Catch exc As Exception
lblError.text = exc.toString
End Try
End Sub
ICS 文件下载页面
Sub Page_Load(Src As Object, E As EventArgs)
Try
'** GET ICS FILE FROM SESSION VARIABLE
Dim ICSFile As String = Session("ICSFile").ToString
Response.ContentType = "text/calendar"
Response.AddHeader("content-disposition", "attachment; filename=CalendarEvent1.ics")
Response.Write(ICSFile)
Response.End()
Catch exc As Exception
lblError.text = exc.toString
End Try
End Sub
HTML
asp:按钮 ID="btnAddToCalendar" runat="server" OnClick="AddToCalendar" OnClientClick="javascript:DownloadICS();" Text="Add To Calendar" />
JavaScript
<script type="text/javascript" language="javascript">
function DownloadICS() {
popUp("ics_download.aspx", 300, 300);
}
function popUp(url, width, height) {
if (window.featWin && !window.featWin.closed) {
window.featWin.close();
featWin = window.open(url, "win", 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
selfWin = self;
featWin.focus();
}
else {
featWin = window.open(url, "win", 'toolbar=0,location=1,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=' + width + ',height=' + height);
selfWin = self;
featWin.focus();
}
}
function closeWin() {
if (window.featWin && !window.featWin.closed) {
window.featWin.close();
}
}