ASP.Net(VB) - 使用Response.BinaryWrite下载文件并不起作用

时间:2017-11-09 20:43:28

标签: asp.net vb.net devexpress

请注意,我在以下代码中使用DevExpress的ASPxCallback控件(doc:https://documentation.devexpress.com/AspNet/DevExpress.Web.ASPxCallback.class

这是我的问题:我试图允许用户下载文件,但它不起作用。我不能使用TransmitFile,因为它来自数据库所以我使用了BinaryWrite。 这是我目前的实施:

1)首先,我从代码隐藏中填充了一个表,如下所示:

For Each version As Fichier In fichiers.OrderByDescending(Function(x) x.LastModifiedDate)
                Dim row As New TableRow()
                Dim cellFichier As New TableCell()
                Dim cellDate As New TableCell()
                Dim cellDownload As New TableCell()
                cellFichier.Text = idFile.ToString()
                cellDate.Text = version.LastModifiedDate.ToString()
                cellDownload.Text = String.Format("<img class='img-small' src='icons/DownloadFile_inverse_32x.png' onclick=""downloadRevisions({0});"" />", version.ID)
                row.Cells.Add(cellFichier)
                row.Cells.Add(cellDate)
                row.Cells.Add(cellDownload)
                tblFileVersions.Rows.Add(row)
                idFile = idFile + 1
            Next

2)调用以下javascript方法:

function downloadRevisions(fileId) {
  callbackDownloadRevision.PerformCallback(fileId);
}

3)最后,在代码隐藏中调用此回调方法:

 Protected Sub OpenEncryptedRevision(source As Object, e As CallbackEventArgs)
    Dim fileID As Integer
    If Integer.TryParse(e.Parameter, fileID) Then
        Dim file As Fichier = Nothing
        Dim decryptedFileBytes As Byte() = DecryptRevision(fileID, file)
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ClearHeaders()
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.AddHeader("content-disposition", "inline; filename=" + file.FileName)
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.ContentType = GestionnaireFichiers.ContentType(file.FileName.Substring(file.FileName.LastIndexOf(".", StringComparison.Ordinal)))
        HttpContext.Current.Response.BinaryWrite(decryptedFileBytes.ToArray())
        HttpContext.Current.Response.Flush()
        HttpContext.Current.Response.End()
    End If
End Sub

我逐行调试了每个功能,一切顺利。从数据库中正确检索文件,标题似乎正常。我在另一个项目中使用相同的方法,它完美地工作。 当最后一个方法结束时,没有任何反应。我很确定这是一个回调问题,但我真的无法弄清楚它的位置。

我不知道它是否有用,但ASPxCallback有html:

<dx:ASPxCallback runat="server" ClientInstanceName="callbackDownloadRevision" ID="CallbackDownloadRevision" OnCallback="OpenEncryptedRevision" ></dx:ASPxCallback>

如果我能提供任何有助于我的信息! 先感谢您, Evanzyker

0 个答案:

没有答案