Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将文件发送到浏览器/response.write替代文件_Javascript_Asp.net_Vb.net_Vb.net 2010 - Fatal编程技术网

Javascript 将文件发送到浏览器/response.write替代文件

Javascript 将文件发送到浏览器/response.write替代文件,javascript,asp.net,vb.net,vb.net-2010,Javascript,Asp.net,Vb.net,Vb.net 2010,在我的aspx页面上,我有一个动态创建的指向存储在Oracle数据库中的文档的链接菜单 当用户单击菜单中的链接时,它调用一个JScript函数,该函数调用代码隐藏页面中的WebMethod修饰的vb.net过程。此函数被传递元素ID,该元素ID用于查询Oracle数据库并返回相应的blob对象,该对象是用户请求的文档 当我测试这个时,我将它连接到一个按钮上,对所有内容进行硬编码,并能够将文档返回到浏览器,该浏览器可以正常工作。下面是我用来将blob对象返回到浏览器的内容 Response.Cle

在我的aspx页面上,我有一个动态创建的指向存储在Oracle数据库中的文档的链接菜单

当用户单击菜单中的链接时,它调用一个JScript函数,该函数调用代码隐藏页面中的WebMethod修饰的vb.net过程。此函数被传递元素ID,该元素ID用于查询Oracle数据库并返回相应的blob对象,该对象是用户请求的文档

当我测试这个时,我将它连接到一个按钮上,对所有内容进行硬编码,并能够将文档返回到浏览器,该浏览器可以正常工作。下面是我用来将blob对象返回到浏览器的内容

Response.Clear()
Response.ClearContent()
Response.ClearHeaders()

Response.Buffer = True
Response.AddHeader("Content-Disposition", "attachment; filename=" + docname)
Response.ContentType = "application/pdf"

Response.BinaryWrite(bytes)
现在,我已经将代码移到了可调用方法中。我刚刚了解到,我不能使用Response将从数据库中提取的文件写入浏览器。或者如果可以做到,我也不知道怎么做。下面是下面的代码

ASPX页面

VB.net模块

.ashx代码


不能这样做的原因是,您现在处于一个XML HTTP请求(更好地称为AJAX)的上下文中,除非您使用了hack或XMLHttpRequest2中的新特性,否则无法将二进制数据直接写入响应

如果您不想求助于黑客攻击或冒着在某些浏览器中得不到广泛支持的风险,另一种选择是简单地从Javascript打开一个新窗口,并将文档id作为url中的参数传递。比如:

function GetDocument(src) {
   window.open('FileHandler.ashx?docId='+src);
}
然后在你现在做的时候流式处理PDF文件


FileHandler.ashx可以是一个Http处理程序,或者如果您想保持它的简单性,请将其设置为一个普通的
aspx
页面,从Request.QueryString获取id并流式处理PDF。

这解决了我的问题。我对XMLHttpRequest2有点困惑,所以我最后添加了一个.ashx文件,因为它更容易理解。我将在上面添加我的代码。啊,这解决了我将excel文件流式传输到的问题。谢谢。
function GetDocument(src) {
 PageMethods.wbmGetDocument(src);
}
Module modGetDoc_Frm

<WebMethod()>
    Public Sub wbmGetDocument(ByVal sID As String)

        Dim dr As OracleDataReader = Get_DataReader(sSql_GetDocument(sID))
        Dim blob As OracleBlob = dr.GetOracleBlob(1)

        Dim bytes(blob.Length) As Byte : blob.Read(bytes, 0, blob.Length)

        Dim doctype As String = dr(2).ToString()
        Dim docname As String = dr(3).ToString()
        
      *** Everything below won't work in module is there an alternative or what am I doing wrong***
        Response.Clear()
        Response.ClearContent()
        Response.ClearHeaders()
        Response.Buffer = True
        
        Response.AddHeader("Content-Disposition", "attachment; filename=" + docname)
        Response.ContentType = "application/pdf"
        Response.BinaryWrite(bytes)


    End Sub


End Module
function GetDocument(id) {
    window.open('FrmDocHandler.ashx?ID=' + id);
}
 Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim sID As String = context.Request.QueryString("id")

        Dim fileName As String
        Dim fileType As String
        Dim bytes() As Byte

        bytes = Get_Blob(fileName, fileType, sSql_GetDocument(sID))
        context.Response.Clear()
        'clear the content of the browser
        context.Response.ClearContent()
        context.Response.ClearHeaders()
        context.Response.Buffer = True

        context.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName)

        context.Response.ContentType = GetMIMEType(fileType)

        context.Response.BinaryWrite(bytes)
function GetDocument(src) {
   window.open('FileHandler.ashx?docId='+src);
}