Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 190
  • Last Modified:

Simply... create a html file on disk with some data from a textbox

Hi,

I have a value in my code behind called "content".
I want to create a html file on disk with the value of "content" in it.

Any ideas.

Thanks, Lk
0
leekey
Asked:
leekey
  • 2
2 Solutions
 
ihenryCommented:
You mean to server-side disk? pretty simple.

'-- aspx
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="Page4.aspx.vb" Inherits="WebVB.Page4" ValidateRequest="False" %>
<html>
      <head></head>
      <body>
            <form id="Form1" method="post" runat="server">
                  <asp:TextBox id="TextBox1" runat="server" TextMode="MultiLine" Height="160px" Width="400px"></asp:TextBox>
                  <br>
                  <br>
                  <asp:Button id="Button1" runat="server" Text="Save Html to Disk"></asp:Button>
            </form>
      </body>
</html>

'-- aspx.vb
Imports System.IO

Public Class Page4
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim sw As StreamWriter = New StreamWriter("D:\Projects\WebVB\output.html")
        sw.Write(TextBox1.Text)
        sw.Close()
    End Sub
End Class


NOTE:
1. In Page directive, ValidateRequest attribute must be set to false. But it opens an attack surface, so make sure you can handle that.
2. Security context must have propert NTFS permission to the destination folder in where the html output is being written.
3. Unless you intend to give full trust level to your web application, it must have FileIOPermission permission code access security policy (if you don't understand this, don't worry. The code should be able to execute without problem on your machine).
0
 
b1xml2Commented:
ASPX
===
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm5.aspx.vb" Inherits="b1xml2.ExpertExchange.VB.WebForm5"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm5</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="FlowLayout">
   <form id="Form1" method="post" runat="server">
   <div>
      <asp:DropDownList ID="ddlCountry" Runat="server">
      <asp:ListItem></asp:ListItem>
      <asp:ListItem>Russia</asp:ListItem>
      <asp:ListItem>United Stated</asp:ListItem>
      </asp:DropDownList>
      </div>
      <div><asp:TextBox ID="txtItem" Runat="server" /></div>
      <asp:Button ID="btnSubmit" Runat="server" Text="Submit" />
 </form>

  </body>
</HTML>


Code Behind
========
Imports System.IO

Public Class WebForm5
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents ddlCountry As System.Web.UI.WebControls.DropDownList
    Protected WithEvents txtItem As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnSubmit As System.Web.UI.WebControls.Button

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Private save As Boolean

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here

    End Sub




    Protected Overrides Sub Render(ByVal writer As System.Web.UI.HtmlTextWriter)
        If Me.save Then
            Dim sw As New StringWriter
            Dim htw As New HtmlTextWriter(sw)
            MyBase.Render(htw)
            Dim html As String = sw.ToString()
            sw.Close()
            htw.Close()
            Dim stm As New StreamWriter(Server.MapPath("data.html"))
            stm.Write(html)
            stm.Flush()
            stm.Close()
            writer.Write(html)
        Else
            MyBase.Render(writer)
        End If

    End Sub

    Private Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
        save = True
    End Sub
End Class


Html File (example after data selection and entry was made)
========================================

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
  <HEAD>
    <title>WebForm5</title>
    <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
    <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
    <meta name=vs_defaultClientScript content="JavaScript">
    <meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
  </HEAD>
  <body MS_POSITIONING="FlowLayout">
   <form name="Form1" method="post" action="WebForm5.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTIwNDQ4OTM1Mjs7PswKAHQeZDe16Ds9We6lLhswHizY" />

   <div>
      <select name="ddlCountry" id="ddlCountry">
      <option value=""></option>
      <option selected="selected" value="Russia">Russia</option>
      <option value="United Stated">United Stated</option>

</select>
      </div>
      <div><input name="txtItem" type="text" value="testd" id="txtItem" /></div>
      <input type="submit" name="btnSubmit" value="Submit" id="btnSubmit" />
 </form>

  </body>
</HTML>

0
 
b1xml2Commented:
as you can notice, the selection on the dropdownlist and textbox value is shown in the saved html
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now