ADODB Stream: Download Text File Issues - asp-classic

I am in a situation where a stream connection is called when the user clicks a download button. When the download button is submitted, a function is called up in ASP, where the download code is executed on the file BD_Test1.txt. The contents of this text file are simply TEST 1.
The file downloads as normal, however when opening it, all of the HTML code is on the text file.
HTML Code
<html>
<head>
<title>Bulk Download Zipper</title>
<!--#include file="BD_Download.asp"-->
</head>
<body>
<center><h1>Bulk Download Zipper</h1></center>
<br><br>
<p>Please click "Download" to zip the files.</p>
<form method="post">
<br><input type = "submit" name="zipFile" value = "Download"/><br>
</form>
<br><br>
<%
If (Request.Form("zipFile") <> "") Then
Call downloadFile()
End If
%>
</body>
</html>
ASP Code
<%
Function downloadFile()
Dim download_File, download_Remove, download_Autoremove
Set download_File = Server.CreateObject("ADODB.Stream")
download_File.Type = 1
download_File.Open
download_File.LoadFromFile("E:\inetpub\wwwroot\cdc\wnyaccc_erie_niagara_trd-2\youkergav\BulkDownload\resumes\BD_Test1.txt")
Response.AddHeader "Content-Transfer-Encoding", "binary"
Response.AddHeader "Content-Description", "File Transfer"
Response.AddHeader "Content-Disposition", "attachment; filename=Resumes.txt"
Response.CharSet = "UTF-8"
Response.ContentType = "text/plain"
Response.BinaryWrite download_File.Read
download_File.Close
Set download_File = Nothing
End Function
%>
Contents of Downloaded Text File
<html>
<head>
<title>Bulk Download Zipper</title>
</head>
<body>
<center><h1>Bulk Download Zipper</h1></center>
<br><br>
<p>Please click "Download" to zip the files.</p>
<form method="post">
<br><input type = "submit" name="zipFile" value = "Download"/><br>
</form>
<br><br>
TEST 1
</body>
</html>
As you can see, the contents of the text file are being inserted where the downloadFile() function is called. Is it possible to use a stream connection is this way?

Your downloadFile function is being called after the page has already begun getting served to the client. ASP is simply injecting the result of your function. You will need to override the entire output on POST. Output the html on GET, and output the text file on POST:
<%
If (Request.Form("zipFile") <> "") Then
Call downloadFile()
Else%>
<html>
<head>
<title>Bulk Download Zipper</title>
<!--#include file="BD_Download.asp"-->
</head>
<body>
<center><h1>Bulk Download Zipper</h1></center>
<br><br>
<p>Please click "Download" to zip the files.</p>
<form method="post">
<br><input type = "submit" name="zipFile" value = "Download"/><br>
</form>
<br><br>
</body>
</html>
<%
End If
%>

Related

Pass Variable from Content page to Master Page in classic ASP

I am new to classic ASP and I am trying to create a Master Page with variable placeholders and fill the information on that page with variables that are worked on a content page.
my master page looks like this:
<html>
<head>
<title>Template Loaded Properly</title>
</head>
<body>
<% call BodyContent %>
<span>Title: <% //place title here %></span>
<span>Content: <% //place content here %></span>
</body>
</html>
and the content page like this:
<!--#include virtual="/templates/TEMPLATE.html" -->
<% sub BodyContent %>
var Title = "This is the title"
var Content = "Here goes some content"
<% end sub %>
Any help will be appreciated.
Once you include the page with the variables, you can treat them as if they were created right then and there (because in a sense they are created right then and there, at least from the server's point of view). You do need to make the variables global in scope [read: dim them outside the sub], unless you want to list all of them when calling your BodyContent sub. (Personally, I don't see the point, but some people are unreasonably allergic to global variables.)
<%
dim Title, Content
sub BodyContent
Title = "This is the title"
Content = "Here goes some content"
end sub
%>
<body>
<% call BodyContent %>
<span>Title: <%=Title%></span>
<span>Content: <%=Content%></span>
</body>
One caveat, though: include files are processed long before the code, so you can't vary what file is included. In other words, don't do this:
<%If x = a Then%>
<!-- #include virtual="/templateA.inc" -->
<%Else%>
<!-- #include virtual="/templateB.inc" -->
<%End If%>
The result of trying something like that is that both templateA and templateB will be included. If you need conditional includes, look into using FileSystemObject to read the content of the appropriate template, and then using Execute to, well, execute it.

Stuck with classic ASP form submission

I am new to classic asp. I have developed a simple asp form where I am doing a form submission to access database. I got a code to do a form validation on fields but the code works only when I you same asp code. When I change the form method to different asp file, validation doesn't work.
Form.asp
....code
<%
Dim send, txtengineer, txtcaseid, txtassetno, txtusername, txtgroup, txtLOB, txtmodel, txtsim, countError
send = Request.Form("submit")
txtengineer = Request.Form("engineer")
txtcaseid = Request.Form("caseid")
txtasset = Request.Form("asset")
txtusername = Request.Form("username")
txtgroup = Request.Form("group")
txtLOB = Request.Form("lob")
txtmodel = Request.Form("model")
txtsim = Request.Form("sim")
countError = 0
%>
<form method="post" action="form.asp">
....
<%
if send <> "" AND txtcaseid = "" then
Response.Write "<span class=""message""> << Enter Case no.</span>"
countError = countError + 1
end If
%>
DB.asp
This is the code where I want to submit the collected field value. Which is working fine. I have forced to keep wherein I am want to do
I want to validate first then submit the form afterwards. I know I am doing something stupid.
Need some guidance to fix the code blocks.
Thanks.
The problem with having the form submit to a different page is, what do you do if there are errors? Tell the user to hit their "back" button? For that reason alone, I'd recommend putting the DB code in the same page as the form. If you can't do that, you need to move your server-side validation to the DB page. The client-side validation would still be on the form page. (Note that if you're only going to do one type of validation, it must be server-side.)
If you do everything in one page, the basic structure would look like this:
<html><head>[etc.]
<%
Const errstr = "<span class='err'>*</span>"
Dim txtengineer, txtcaseid '[etc.]
Dim counterror(8) '- 8 = number of fields
counterror(0) = 0
If Request.Form <> "" Then
txtengineer = Request.Form("engineer")
'etc. with other fields
Validate
If counterror(0) = 0 Then
SavetoDB
Response.Redirect "Success.asp"
End If
End If
Sub Validate()
If txtengineer = "" Then
counterror(1) = 1
counterror(0) = counterror(0) + 1
Else
'do whatever other cleanup/validation you need
End If
'etc. for other fields
End Sub
Sub SavetoDB()
'code to write stuff to database goes here
End Sub
%>
<script>
function validate(){
//client-side validation goes here
}
</script>
</head>
<body>
<%
If counterror(0) > 0 Then
Response.Write "<p>Please fill out the required fields (*) below.</p>"
End If
%>
<form method="post" action="form.asp">
<p>Engineer: <input type="text" name="engineer" value="<%=txtengineer%>" size="30" maxlength="50">
<%If counterror(1) = 1 Then Response.Write errstr%>
</p>
[etc. with other fields]
<p><input type="submit" name="Btn" value="Submit" onclick="validate();"></p>
</form>
</body>
</html>

How to pass value from asp to aspx

I am in asituation ,where i have to redirect from asp to aspx page with parameter.I tried a lot of option,but the redirection is working ,but i couldnt the value in my aspx.cs page.
Below is my asp
Dim objASPError,strRemoteIP
Set objASPError = Server.GetLastError
The below is not working with parameter
'Response.Status = "301 Moved Permanently"
'Response.AddHeader "Location", "/404.aspx? err="+objASPError
'Response.End
I even tried by setting the hiddenn value and accessed the form element in aspx.cs
document.getElementById("hdnErr").value=objASPError
The below is also not working with objASPError
response.Redirect("/404.aspx?err="&objASPError)
<html>
<head>
<title></title>
</head>
<body>
<form method="post" action="404.aspx">
<input type="hidden" id="hdnErr" />
</form>
</body>
</html>
My aspx.cs
Label lbl = new Label();
Exception ex = Server.GetLastError();
string message=string.Empty;
if(Request.Form["hdnErr"] != null)
{
message = Request.Form["hdnErr"].ToString();
}
if (Request.QueryString["err"] != null)
{
String temp = Request.QueryString["err"].ToString();
lbl.Text = temp;
}
Thanks
Question
I understand sending the values through querystring is not a best option ... How to send error object from asp to aspx..
try changing
<input type="hidden" id="hdnErr" />
to
<input type="hidden" name="hdnErr" />
Also, Server.GetLastError returns an object so you can't combine it with a string. Take a look at the example in the msdn doc.
ASPError Object

passing parameters to .aspx page using renderpartial

in my index.aspx page i want to render another module.aspx page using renderpartial
which then render a .htm file
depanding on which parameter is passed from index.aspx (it would be number ie 1,2 etc ,so as to call different different .htm file everytime depending on the parameter)
1).
now i want Index.aspx page to render module.aspx and pass it a parameter(1,2,3,etc)
[the parameters would be passed programatically (hardcoded)]
and
2).
mudule.aspx should catch the parameter and depending on it will call .htm file
my index.aspx has
<% ViewData["TemplateId"] = 1; %>
<% Html.RenderPartial("/Views/Templates/MyModule.aspx", ViewData["TemplateId"]); %>
and module.aspx contains
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<script type="text/javascript" src="/Scripts/jquery-1.3.2.js"></script>
<script type="text/javascript" src="/Scripts/Service.js"></script>
<script type="text/javascript">
debugger;
var tid = '<%=ViewData["TemplateId"] %>';
$.get("/Templates/Select/" + tid, function(result) {
$("#datashow").html(result);
});
</script>
<div id="datashow"></div>
this is my controller which is called by $.get(....) (see code)
public ActionResult Select(int id)
{
return File("/Views/Templates/HTML_Temp" +id.ToString()+".htm" , "text/html");
}
and finally
my .htm file
<div id="divdata" class="sys-template">
<p>Event Title:<input id="title" size="150" type="text"
style="background-color:yellow;font-size:25px;width: 637px;"
readonly="readonly" value="{{title}}" />
</p>
<p>Event Description:<input type="text" id="description" value="{{ description }}"
readonly="readonly" style="width: 312px" /></p>
<p>Event Date: <input type="text" id="date" value="{{ date }}" readonly="readonly"
style="width: 251px"/></p>
<p>Keywords : <input type="text" id="keywords" value="{{keywords}}" readonly="readonly" /></p>
</div>
<script type="text/javascript">
Sys.Application.add_init(appInit);
function appInit() {
start();
}
</script>
start() is javascript method which is in file Service.js
when i run this programm it gives me error
js runtime error: 'object expected'
and debugger highlighted on
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/**xhtml**1-strict.dtd">
pls help me solve the problem
Use like this <% Html.RenderPartial("/Views/Templates/MyModule.ascx", Model); %> for passing the values using Model to a partial view MyModule.ascx. You can also use the method Html.RenderAction
When you use RenderPartial, you are by default passing the Model of your Index.aspx. Your partial view can then be of the same type. You can then use Model.MyParameter to find out which htm file you should be rendering. Otherwise you can pass it in the object parameter of the RenderPartial, and query that object inside of your partial view.

POST data getting 'lost' somewhere

UPDATE
So it turns out internet exploder's stranglehold on "security" to "make up" for being so bad at security was causing my problems. I should have checked that out first haha. Thanks everyone for the input, it has given me ideas on how to optimize my application :D
I am writing a web app (in ASP.NET 3.5) that integrates with a platform app. The platform app takes the user's credentials and puts them into an "empty" HTML page that consists of a form with hidden items containing said credentials and POSTS to the webapp (default.aspx):
<HTML>
<HEAD>
<SCRIPT LANGUAGE=JSCRIPT>
function OnLoad(){
try {
document.form1.submit();
}
catch(e){
}
}
</SCRIPT>
</HEAD>
<BODY OnLoad="OnLoad()">
<FORM ACTION="http://localhost:51816/gs_ontheweb/default.aspx" METHOD=POST NAME=form1 TARGET="_NEW">
<INPUT TYPE="HIDDEN" NAME="ClientID" VALUE="123456">
<INPUT TYPE="HIDDEN" NAME="Password" VALUE="2830088828">
<INPUT TYPE="HIDDEN" NAME="PracType" VALUE="051">
<INPUT TYPE="HIDDEN" NAME="Encrypt" VALUE="12345620081111">
</FORM>
</BODY>
</HTML>
When my default.aspx page gets loaded up, it calls the following function:
Dim ClientID As String = Request.Form("ClientID")
Dim PassWord As String = Request.Form("Password")
Dim PracType As String = Request.Form("PracType")
Each one of them result in empty strings. Any ideas on why this is happening? Thanks in advance.
EDIT: Is there something I need to configure in my web.config file to make this work properly? Request.Params("<param name>") does not work.
Your issue is the "Target" property on the Form. Why is this here?
(I also took the liberty of cleaning your HTML up a little)
<html>
<head>
<title>Test JS Post</title>
<script type="text/javascript" language="javascript">
<!--
function OnLoad(){
try
{
alert("Posting...");
document.form1.submit();
}
catch(e)
{
alert("ERROR!");
alert(e);
}
}
//-->
</script>
</head>
<body onload="OnLoad()">
<form action="http://localhost:49684/Default.aspx" method="post" name="form1">
<input type="hidden" name="ClientID" value="123456" />
<input type="hidden" name="Password" value="2830088828" />
<input type="hidden" name="PracType" value="051" />
<input type="hidden" name="Encrypt" value="12345620081111" />
<h1>This is in the form. Submit me here:</h1><input type="submit" value="foo" />
</form>
</body>
</html>
In the code behind of Default.aspx:
Private Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
For Each value As String In Request.Form.Keys
Debug.WriteLine(String.Format("{0} = ""{1}""", value, Request.Form.Item(value)))
Next
End Sub
That HTML is just on the user's harddrive? Maybe the browser security won't let that POST because it's deemed to be a risk.
As a test -- take that exact HTML file and put it on your webserver and then browse to it. If it works, might be the browser refusing to send the data. You could check with Fiddler (for IE) or Firebug in FireFox.
Why not use System.Net.WebClient?
Some sample code (sorry, it's C#. Looks like your looking for VB. I can't translate quickly.)
System.Net.WebClient wc = new System.Net.WebClient();
byte[] b;
byte[] res;
string formdata = "text=test text&password=secret&checkbox=on&textarea=a longer text sentence&submit=submit";
// encode the form data string into a byte array
b = System.Text.Encoding.ASCII.GetBytes(formdata);
// set the content type for a form
wc.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
// POST and get data
res = wc.UploadData("http://localhost:51816/gs_ontheweb/default.aspx", b);
//convert the return page from byte[] to ascii
string s = System.Text.Encoding.ASCII.GetString(res);

Resources