Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   ASP.Net (http://www.xtremevbtalk.com/asp-net/)
-   -   ASP.Net download issue (http://www.xtremevbtalk.com/asp-net/327983-asp-net-download-issue.html)

YaskinForrit 12-09-2015 11:13 AM

ASP.Net download issue
 
My ASP.Net page contains several tabs, each with a drop down list that contains files available for download.

I am using a separate page (DownloadFile.ashx) to handle the download and calling it with Response.Redirect.

Download Function
Code:

Public Shared Sub DownloadFile(ByVal FileURL As String)
Dim fileName As String

    'Get full fileURL if leading character is '~' or '.'
    If FileURL.Substring(0, 1) = "~" Or FileURL.Substring(0, 1) = "." Then
        FileURL = New System.IO.FileInfo(HttpContext.Current.Server.MapPath(FileURL)).FullName
    End If

    'Pass file URL to Document.aspx
    System.Web.HttpContext.Current.Response.Redirect("Document.aspx?fileURL=" & FileURL.ToString())
    Return
End Sub

DownloadFile.ashx Page_Load
Code:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
Dim response As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
Dim request As System.Web.HttpRequest = System.Web.HttpContext.Current.Request
Dim fileURL As String = ""
Dim fileName As String = ""
Dim fileType As String = ""
Dim contentType As String = "text/plain"

        fileURL = request.QueryString("fileURL").ToString()
        fileName = fileURL.Substring(fileURL.LastIndexOf("/") + 1)
        fileType = fileName.Substring(fileName.LastIndexOf(".") + 1).ToUpper

        'get content type from file type
        Select Case fileType
          Case "PDF"
                contentType = "application/pdf"
          Case "PNG"
                contentType = "image/png"
          Case "JPG"
                contentType = "image/jpeg"
          Case "HTML", "HTM"
                contentType = "text/html"
          Case "XLSX", "XLS"
                contentType = "application/vnd.xls"
          Case "DOCX", "DOC"
                contentType = "application/ms-word"
        End Select

        If Not fileName Is Nothing Then
          response.ClearContent()
          response.Clear()
          response.ContentType = contentType
          response.AddHeader("Content-Disposition", "attachment; filename=" & fileName & ";")
          response.TransmitFile(fileURL)
          response.Flush()
          response.End()
        End If
End Sub

I have tried using HTTPApplication.CompleteRequest instead of Response.End and I have even tried moving the DownloadFile.ashx code into my download function.

The error remains the same.

My download works but then every time I get postback, it causes the file to be re-downloaded.

PlausiblyDamp 12-09-2015 04:19 PM

When you say "...every time I get postback, it causes the file to be re-downloaded." what url are you posting back to? Is it going back to the download handler or somewhere else?

YaskinForrit 12-10-2015 03:07 AM

Quote:

Originally Posted by PlausiblyDamp (Post 1409291)
When you say "...every time I get postback, it causes the file to be re-downloaded." what url are you posting back to? Is it going back to the download handler or somewhere else?

The site had 5 tabs. when I click on one of those tabs, the file re-downloads

Each of the tabs has 2 or more drop down lists. when I click on any of these, the file re-downloads

YaskinForrit 12-10-2015 10:28 AM

I have identified why my downloads keep on recurring.

My code to call the download function resets the selected index to 0
Code:

  Protected Sub DownloadQuarter()
    Dim fileURL As String
    If QuarterlyDownloadDropDown.SelectedIndex = 0 Then Exit Sub
    fileURL = GetURL(QuarterlyDownloadDropDown.SelectedValue, "QR")
    QuarterlyDownloadDropDown.SelectedIndex = 0
    DownloadAudit(fileURL, "Quarterly")
    DownloadFile(fileURL)
  End Sub

even though I am setting the SelectedIndex to 0, the dropdownlist selection does not change.

If I comment out 'DownloadFile(fileURL)', the dropdownlist selection gets reset.

PlausiblyDamp 12-14-2015 06:20 AM

Have you tried Server.Transfer rather than Response.Redirect to get the file to download? Response.Redirect will change the URL the browser is looking at so a postback will presumably be triggering the download again.

Then again it is a while since I have done anything with Web Forms so I might be wrong about Server.Transfer anyway...


All times are GMT -6. The time now is 09:38 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.