更新:
给出的大多数答案只是将返回的文档注入(inject)到页面的 html 中的某种方式,但这似乎不起作用。如果您能看到 aspx 代码到底在做什么,这可能会有所帮助:
Try RenderDocument = 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 Catch ex As Exception ResponseWithMessage("ERROR: " & ex.Message) End Try
- $.post() 函数的返回类型有什么意义?在我的代码中,我使用“html”,但实际上它可能是返回的文件。
====================
原始问题:
场景是我想加载一个 html 页面,该页面将调用特定的 ASP.Net 脚本,该脚本根据 session 中存储的一些数据返回一个文档。它的功能是无关紧要的,本质上它以 http 响应的形式返回文档,但是根据发生的情况,它可能会返回几个错误字符串之一。
html 页面显示一个正在加载的 gif 图像,然后当返回文档或错误消息时,它需要适本地处理这些图像。
我可以让页面毫无问题地显示错误消息,但我不知道如何最好地处理返回的文档请求,目前它只是打开一个新窗口并再次调用该网址,但是很笨重,并且再次调用整个文档检索过程(同步!),这违背了使用良好的 ajax 样式加载的目的。
这是我的 JQuery 代码:
$(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();
}
这是我的 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>
我们一如既往地高度重视您的帮助。
谢谢
最佳答案
更新:我认为问题在于你试图用一种方法做太多事情。我会将其分为两个操作:检查文档是否可用并下载文档。让您通过 POST 调用的方法(GET 也可以工作,因为您没有传递任何数据)执行存在性检查,然后创建指向执行实际下载的文档处理程序 URL 的链接。将链接添加到页面并以编程方式单击它以调用下载处理程序。下载处理程序将包含您现有的代码,如果在不存在的文档上调用,则仅返回错误页面。请注意,后者在正常操作中不会发生,但如果有人为下载的文档添加了书签并将其删除,则可能会发生。
此外,我很想使用 JSON 作为返回值而不是 HTML,以便更轻松地处理错误。
$(function() {
$.get('checkfordocument.aspx',null,function(data) {
if (!data.Success) {
UpdateControls(data.Message);
}
else {
UpdateControls('');
$("<a href='"
+ data.Url
+ "' target='_blank' class='docLink'>"
+ "Click here if your document does not start downloading</a>")
.appendTo('#txtInfo')
.trigger('click');
}
,'json');
});
关于asp.net - 使用 JQuery 检索物理文件,然后将其显示在浏览器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1193300/