ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button
Article:
  Remote Scripting with IFRAME
Subject:   Using IFRAME to download a content from the server
Date:   2004-09-27 05:06:42
From:   Shyamal
Hi,


I am using frames on the page.I have a asp page where I upload a file using Request.BinaryRead and insert into a database the file data. While inserting the data i do certain validation based on which I insert them into an error table if they fails the validation. Once this returns if there are some data inserted into the error table then we need to download them by enabling a button. This is done by retrieveing the data from the server and writing them to the Response object and download them using
Response.ContentType = "application/txt"
Response.AddHeader "content-disposition","attachment;filename=Errors.txt"


Till here everything is working fine. But once the browser is forced to download and user saves the download content to some location then I am not able to access any of the elments on that page like the buttons or other controls. It gives a Javascript error "unspecified Error"


I tried to solve it using a IFrame in the page and now the that javascript error has gone but now no javascript is being fired at all on clicking the buttons on the page.


Can you kindly let me know the workaround to come over this problem.


The Code is something as follows:



Upload.asp
-------------------------------------------------
<%
Dim intDict


'This function takes the Request.BinaryRead data as parameter
and parses it to create a dictionary objects
'A name-value collection which is later used to retrieve the
various form field data


Private Sub getData(rawData)
Dim sSeparator
sSeparator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)

Dim lenSeparator
lenSeparator = LenB(sSeparator)


Dim currentPos
currentPos = 1
Dim inStrByte
inStrByte = 1
Dim value, mValue
Dim tempValue
tempValue = ""


While inStrByte > 0
inStrByte = InStrB(currentPos, rawData, sSeparator)
mValue = inStrByte - currentPos


If mValue > 1 Then
value = MidB(rawData, currentPos, mValue)


Dim begPos, endPos, midValue, nValue
Set intDict = Server.CreateObject("Scripting.Dictionary")


begPos = 1 + InStrB(1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))
nValue = endPos


Dim nameN
nameN = MidB(value, begPos, endPos - begPos)


Dim nameValue, isValid
isValid = True


If InStrB(1, value, stringToByte
("Content-Type")) > 1 Then


begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))


If endPos = 0 Then
endPos = begPos + 1
isValid = False
End If


midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "FileName", trim(byteToString(midValue))


begPos = 14 + InStrB(endPos + 1, value,
stringToByte("Content-Type:"))
endPos = InStrB(begPos, value, ChrB(13))


midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "ContentType", trim(byteToString(midValue))


begPos = endPos + 4
endPos = LenB(value)


nameValue = MidB(value, begPos, ((endPos - begPos) - 1))
Else
nameValue = trim(byteToString(MidB(value, nValue + 5)))
End If


If isValid = True Then


intDict.Add "Value", nameValue
intDict.Add "Name", nameN


oDict.Add byteToString(nameN), intDict
End If
End If


currentPos = lenSeparator + inStrByte
Wend
End Sub



'This function converts the string data to a binary format
Private Function stringToByte(toConv)
Dim tempChar,ndataLen
For ndataLen = 1 to Len(toConv)
tempChar = Mid(toConv, ndataLen, 1)
stringToByte = stringToByte & chrB(AscB(tempChar))
Next
End Function


'This function converts the binary data to a string format
Private Function byteToString(toConv)
Dim ndataLen
For ndataLen = 1 to LenB(toConv)
byteToString = byteToString & Chr(AscB(MidB(toConv,ndataLen,1)))
Next
End Function


'This function resets teh dictonary objects created
Private Sub Terminate
If IsObject(intDict) Then
intDict.RemoveAll
Set intDict = Nothing
End If
If IsObject(oDict) Then
oDict.RemoveAll
Set oDict = Nothing
End If
End Sub



'Function create a GUID and passed to the VB component for inserting data
Function GetGuid()
Dim oTypeLib
Set oTypeLib = Server.CreateObject("Scriptlet.TypeLib")
GetGuid = oTypeLib.Guid
Set oTypeLib = Nothing
End Function


%>


<%
Response.Buffer = True
Dim oStockControl,oStockSearch,oCAUTTypeList,oDict


Dim nerrCount,bytCount,bytread,sgetFileData,sCautNumber,sbtnType,vBuiGuid


'Create a dictionary object
Set oDict = Server.CreateObject("Scripting.Dictionary")


bytCount = Request.TotalBytes


If bytCount>0 Then
bytread = Request.BinaryRead(bytCount) 'Read the posted data
getData bytread 'Create a dictionary object

'Extract necessary form fields data from the dictonary object
'Get the file data
If oDict.Exists("oFile") Then
sgetFileData = oDict("oFile").Item("Value")
Else
sgetFileData = ""
End If


'Get the button being pressed
If oDict.Exists("sbtnType") Then
sbtnType = oDict("sbtnType").Item("Value")
sbtnType=Left(sbtnType,Len(sbtnType)-2)
Else
sbtnType = ""
End If

'Get the GUID for the transaction
If oDict.Exists("vBuiGuid") Then
vBuiGuid = oDict("vBuiGuid").Item("Value")
vBuiGuid=Left(vBuiGuid,Len(vBuiGuid)-2)
Else
vBuiGuid = ""
End If
End if



'Create an instance of the class to be used to insert the data
Set oStockControl=Server.CreateObject("dobUploadFile.clsUploadFile")


nerrCount=0


'Get the controller types available to populate the Controller type combo


Set oStockSearch = Server.CreateObject("dobStockControl.clsStockControl")


Set oCAUTTypeList = oStockSearch.getCAUList(True)



%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
<LINK REL="STYLESHEET" TYPE="TEXT/CSS" HREF="../STYLESHEET/ntlcss.css"
ENCTYPE="multipart/form-data">
<TITLE>SABS Portal - Upload Suppler File</TITLE>
<!-- #INCLUDE FILE = '../include/utils.js' -->
<script language="javascript">
function checkPath()
{

var selectedControllerType=document.frmUploadSupplierFile.
sCautNumber[document.frmUploadSupplierFile.sCautNumber.
selectedIndex].value;

if(selectedControllerType!="")
{
if(document.frmUploadSupplierFile.oFile.value=="")
{
alert("Please choose a file to upload");
}
else
{

document.frmUploadSupplierFile.
sbtnType.value="Upload";
document.frmUploadSupplierFile.submit();
}
}
else
{
alert("Please select a controller type for the file upload");
}
}
function downLoadErrors()
{
document.frmUploadSupplierFile.action="Download.asp?vBuiGuid=
"+document.frmUploadSupplierFile.vBuiGuid.value;
//document.frmUploadSupplierFile.target="RSIFrame";
document.frmUploadSupplierFile.submit();
}


function checkPreviousErrors()
{
<% if(oStockControl.RetrieveInvErrors()<>"") then %>
//if(confirm("The previous upload produced errors which
must be downloaded.Once downloaded the errors will
be deleted"))
//{
<%
'sFileContent=oStockControl.RetrieveInvErrors()
'Response.Clear()
'Response.Write(sFileContent)
'Response.ContentType = "text/plain"
'Response.AddHeader "content-disposition","
attachment;filename=Errors.txt"
'oStockControl.DeleteInvErrorsDownloaded
(Session("GUID"))
'Response.End
%>
document.frmUploadSupplierFile.errorShow.disabled;

//}
//else
//{
// return;
//}
<% End If%>
}
</script>



</HEAD>
<BODY onload="javascript:checkPreviousErrors();">
<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="1">
<tr><td colspan=2>

<H1>Load Supplier File</H1></td></tr>
<tr><td colspan=2>
<%


' Check whether the Upload file button has been clicked
and then call the Upload method to
' process the file and insert into Manufacturer_Upload_Inventory
table for the correct data
' and the Bulk_Upload_Manufacturer_Inv_Errors for the
erroneous records.


Dim vCorrectRecordCount,vErrCount,rsErrorRecords,sTableEntry


If trim(CStr(sCautNumber))<> "" and trim(CStr(sbtnType))="Upload" Then

vBuiGuid=GetGuid

Set rsErrorRecords=oStockControl.UploadSupplierFile
(Cstr(byteToString(sgetFileData)),
Cint(sCautNumber),cstr(Session("UserID")),vBuiGuid,
vCorrectRecordCount,vErrCount)


'Session("GUID")=vBuiGuid
sTableEntry="<table><tr><td colspan=3>Total number of records
entered: " & vCorrectRecordCount& "</td></tr>" & vbCrLf
sTableEntry=sTableEntry & "<tr><td colspan=3>Number of records
not entered: " & vErrCount & "</td></tr>" & vbCrLf
nerrCount=vErrCount
If nerrCount>0 Then
sTableEntry=sTableEntry & "<tr><td>Error Description</td>
<td><td>Record</td></tr>" & vbCrLf
Do While NOT rsErrorRecords.EOF
sTableEntry = sTableEntry & "<tr><td>" &
rsErrorRecords.fields(2) & "</td><td></td><td>" &
rsErrorRecords.fields(3) & "</td></tr>" & vbCrLf
rsErrorRecords.MoveNext
Loop
sTableEntry = sTableEntry & "<tr><td colspan=3>

 

</td></tr></table>" & vbCrLf


Response.Write(sTableEntry)
End if
End If


%>
</td></tr>
<tr><td colspan=2>

Identify the Controller Type for the STBs

</td></tr>
<tr><td colspan=2>

 

</td></tr>
</table>


<FORM NAME="frmUploadSupplierFile" ACTION="LoadSupplierFile.asp"
METHOD="post" enctype="multipart/form-data">
<TABLE BORDER="0" CELLSPACING="1" CELLPADDING="1">
<tr><td> </td>
<td>


Enter the full path & filename


File location: <INPUT TYPE="file" NAME="oFile"/>



<INPUT TYPE="button" class=ntlbutton VALUE="Load File"
onclick="checkPath()" id=btnUpload name=btnUpload>
<INPUT TYPE="button" class=ntlbutton VALUE="Produce
Error File" id=errorShow name=errorShow
<% if nerrCount=0 then Response.Write("disabled") end if%>
onclick="javascript:downLoadErrors();">
<input type="hidden" name="sbtnType" value="">
<input type="hidden" ID="vBuiGuid" name="vBuiGuid"
value="<%=vBuiGuid%>">
<iframe id="RSIFrame" name="RSIFrame"
style="width:0px; height:0px; border: 0px" src="">
</iframe>


</td>
</tr>
</table>
</FORM>
<%
Terminate
Set oStockControl=Nothing
%>
</BODY>
</HTML>
------------------------------------------------
Download.asp
------------------------------------------------



<%


Dim sFileContent,vBuiGuid,oStockControl

Set oStockControl=Server.CreateObject("dobUploadFile.clsUploadFile")
If Request.QueryString("vBuiGuid")="" or Request.QueryString
("vBuiGuid")=null Then vBuiGuid=null
Else
vBuiGuid=Request.QueryString("vBuiGuid")
End If
sFileContent=oStockControl.RetrieveInvErrors(trim(vBuiGuid))

'Trap any errors
If Err.Number <> 0 Then Call ErrorHandler(Err.Number,
Err.Description, Err.Source)

If sFileContent<>"" Then

Response.Buffer = True
Response.Clear()
Response.Write(sFileContent)

Response.ContentType = "application/txt"
Response.AddHeader "content-disposition","attachment;
filename=Errors.txt"


oStockControl.DeleteInvErrorsDownloaded(trim(vBuiGuid))

'Trap any errors
If Err.Number <> 0 Then Call ErrorHandler(Err.Number,
Err.Description, Err.Source)

'Response.End
Response.Write("<script>window.close</script>")
End If



%>


Kindly let me know the workaround if anyone has come across this problem earlier.


Looking forward for a quick and favourable response from the group


Regards
Shyamal