ASP.Net download issue
ASP.Net download issue
ASP.Net download issue
ASP.Net download issue
ASP.Net download issue
ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue
ASP.Net download issue ASP.Net download issue
ASP.Net download issue
Go Back  Xtreme Visual Basic Talk > > > ASP.Net download issue


Reply
 
Thread Tools Display Modes
  #1  
Old 12-09-2015, 10:13 AM
YaskinForrit YaskinForrit is offline
Newcomer
 
Join Date: Feb 2009
Posts: 6
Default 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.
Reply With Quote
  #2  
Old 12-09-2015, 03:19 PM
PlausiblyDamp's Avatar
PlausiblyDampASP.Net download issue PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

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?
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
  #3  
Old 12-10-2015, 02:07 AM
YaskinForrit YaskinForrit is offline
Newcomer
 
Join Date: Feb 2009
Posts: 6
Default

Quote:
Originally Posted by PlausiblyDamp View Post
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
Reply With Quote
  #4  
Old 12-10-2015, 09:28 AM
YaskinForrit YaskinForrit is offline
Newcomer
 
Join Date: Feb 2009
Posts: 6
Default

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.
Reply With Quote
  #5  
Old 12-14-2015, 05:20 AM
PlausiblyDamp's Avatar
PlausiblyDampASP.Net download issue PlausiblyDamp is offline
Ultimate Contributor

Forum Leader
* Expert *
 
Join Date: Nov 2003
Location: Newport, Wales
Posts: 2,058
Default

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...
__________________
Intellectuals solve problems; geniuses prevent them.
-- Albert Einstein

Posting Guidelines Forum Rules Use the code tags
Reply With Quote
Reply

Tags
contenttype, dim, string, download, fileurl, filename, downloadfile.ashx, file, filetype, response.end, select, type, page, asp.net, fileurl.substring0, function, xlsx, text/html, htm, xls, application/vnd.xls, doc, docx, html, image/jpeg


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
ASP.Net download issue
ASP.Net download issue
ASP.Net download issue ASP.Net download issue
ASP.Net download issue
ASP.Net download issue
ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue ASP.Net download issue
ASP.Net download issue
ASP.Net download issue
 
ASP.Net download issue
ASP.Net download issue
 
-->