UPDATE:
Most of the answers given have just been some way of injecting the returned document into the html of the page, but this does not seem to be working. It might help if you could see what the aspx code is doing after all:
Try
Catch ex As Exception ResponseWithMessage("ERROR: " & ex.Message) End TryRenderDocument = Session("docSelected") If Not String.IsNullOrEmpty(Request.QueryString("download")) Then asDownload = CBool(Request.QueryString("download")) End If If RenderDocument IsNot Nothing Then Dim docBytes() As Byte Dim docExtension As String = "" Dim docFileSize As Long GetBytesAndExtension(docBytes, docExtension, docFileSize) If docBytes IsNot Nothing Then Dim ms As New MemoryStream(docBytes) With ms Dim dataLengthToRead As Long = ms.Length Dim blocksize As Integer If dataLengthToRead >= 5000 Then blocksize = 5000 Else blocksize = dataLengthToRead End If Dim buffer(dataLengthToRead) As Byte Response.Clear() Response.ClearContent() Response.ClearHeaders() Response.BufferOutput = True If asDownload = True Then Response.AddHeader("Content-Disposition", "attachment; filename=" & RenderDocument.Name & "." & docExtension) Else Response.AddHeader("Content-Disposition", "inline; filename=" & RenderDocument.Name & "." & docExtension) End If Response.AddHeader("Content-Length", docFileSize.ToString()) Response.ContentType = "application/octet-stream" While dataLengthToRead > 0 And Response.IsClientConnected Dim lengthRead As Integer = ms.Read(buffer, 0, blocksize) Response.OutputStream.Write(buffer, 0, lengthRead) dataLengthToRead = dataLengthToRead - lengthRead End While Response.Flush() Response.Close() End With Response.End() Else ResponseWithMessage("No Data") End If Else ResponseWithMessage("No Document") End If
- What is the significance of the return type of the $.post() function? In my code I use 'html', but actually it could be a file being returned.
===================
ORIGINAL QUESTION:
The scenario is I want to load a html page which will call a specific ASP.Net script, this script returns a document based on some data stored in the session. It's function is irrelevant, essentailly it returns the document as an http response, however it may return one of several error strings depending on what has occured.
The html page shows a loading gif image and then when the document or error message is returned it needs to handle these appropriately.
I can get the page to display the error messages without a problem, but I don't know how best to handle the document request being returned, at the moment it just opens a new window and calls the url again, but that is clunky and calls the whole document retrieval process again (synchronously!) which defeats the purpose of using a nice ajax style load.
This is my JQuery code:
$(document).ready(function(){
$.post("displaydocument.aspx",null,function(data){
if (data == 'No Document'){
UpdateControls('No document has been specified to load');
}
if (data == 'No Data'){
UpdateControls('No data was found for that document');
}
if (data.indexOf('ERROR',0) != -1) {
UpdateControls(data);
}
window.open("displaydocument.aspx","doc");
window.close;
}
, "html");
});
function UpdateControls(message){
$("#txtInfo").html(message);
$("#imgLoading").hide();
}
This is my html:
<div style="text-align: center; font-family: 'Trebuchet MS';">
<img alt="loading" src="/images/loader64.gif" id="imgLoading" />
<br />
<span style="font-size: small" id="txtInfo">Document is loading...</span>
</div>
Your assistance is as always highly valued.
Thanks