473,386 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

problem in downloading a file and refresh page

i want to download a file which user select from gridview, downloading is completing without problem but after download i want to refresh my page because i do some changes in db .

but when refresh.redirect() occure it give me error that

can't redirect because headers have been sent



following are my code to download file


Expand|Select|Wrap|Line Numbers
  1. FDownLoadFile(objAttachmentInfo.FileName)
  2.  
  3. Response.Redirect(EditUrl("ItemId", ItemId, "Anonymous", "&tab=4"), False)
  4.  
  5.  
  6.  
  7. Sub FDownLoadFile(ByVal fileName As String)
  8.             Try
  9.                 Dim filePath As String = ConfigurationManager.AppSettings.Get("picUploadPath")
  10.                 Dim objFile As New System.IO.FileInfo(filePath + fileName)
  11.                 Dim objResponse As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
  12.  
  13.                 If objFile.Exists Then                   
  14.                     objResponse.ClearContent()
  15.                     objResponse.ClearHeaders()
  16.                     objResponse.AppendHeader("content-disposition", "attachment; filename=""" & objFile.Name.ToString() & """")
  17.                     objResponse.AppendHeader("Content-Length", objFile.Length.ToString())
  18.                     objResponse.ContentType = DownloadFile.GetContentType(objFile.Extension.Replace(".", ""))
  19.                     DownloadFile.WriteFile(objFile.FullName)
  20.                 Else
  21.                     lb_attachment_status.Text = "File not exist."
  22.                     Exit Sub
  23.                 End If
  24.             Catch ex As Exception
  25.                 ProcessModuleLoadException(Me, ex)               
  26.             End Try
  27.         End Sub
  28.  

Expand|Select|Wrap|Line Numbers
  1.  Imports System
  2. Imports System.IO
  3. Imports System.Data
  4. Imports System.Configuration
  5. Imports System.Web
  6. Imports System.Web.Security
  7. Imports System.Web.UI
  8. Imports System.Web.UI.WebControls
  9. Imports System.Web.UI.WebControls.WebParts
  10. Imports System.Web.UI.HtmlControls
  11.  
  12.  
  13. Namespace PLM.Modules.PLMAttachment
  14.     ''' <summary>
  15.     ''' Summary description for DownloadFile
  16.     ''' </summary>
  17.     '''
  18.     Public Class DownloadFile
  19.         Public Sub New()
  20.             '
  21.             ' TODO: Add constructor logic here
  22.             '
  23.         End Sub
  24.  
  25.         Public Shared Function GetContentType(ByVal extension As String) As String
  26.             Dim contentType As String
  27.             If extension.ToLower() = "txt" Then
  28.                 contentType = "text/plain"
  29.             ElseIf extension.ToLower() = "htm" OrElse extension.ToLower() = "html" Then
  30.                 contentType = "text/html"
  31.             ElseIf extension.ToLower() = "rtf" Then
  32.                 contentType = "text/richtext"
  33.             ElseIf extension.ToLower() = "jpg" OrElse extension.ToLower() = "jpeg" Then
  34.                 contentType = "image/jpeg"
  35.             ElseIf extension.ToLower() = "gif" Then
  36.                 contentType = "image/gif"
  37.             ElseIf extension.ToLower() = "bmp" Then
  38.                 contentType = "image/bmp"
  39.             ElseIf extension.ToLower() = "mpg" OrElse extension.ToLower() = "mpeg" Then
  40.                 contentType = "video/mpeg"
  41.             ElseIf extension.ToLower() = "avi" Then
  42.                 contentType = "video/avi"
  43.             ElseIf extension.ToLower() = "pdf" Then
  44.                 contentType = "application/pdf"
  45.             ElseIf extension.ToLower() = "doc" OrElse extension.ToLower() = "dot" Then
  46.                 contentType = "application/msword"
  47.             ElseIf extension.ToLower() = "csv" OrElse extension.ToLower() = "xls" OrElse extension.ToLower() = "xlt" Then
  48.                 contentType = "application/x-msexcel"
  49.             Else
  50.                 contentType = "application/octet-stream"
  51.             End If
  52.             Return contentType
  53.         End Function
  54.  
  55.         Public Shared Sub WriteFile(ByVal strFileName As String)
  56.             Dim objResponse As System.Web.HttpResponse = System.Web.HttpContext.Current.Response
  57.             Dim objStream As System.IO.Stream = Nothing
  58.             Try
  59.                 objStream = New System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read)
  60.                 WriteStream(objResponse, objStream)
  61.                 objStream.Close()                
  62.             Catch ex As Exception
  63.                 objResponse.Write("Error : " & ex.Message)
  64.             Finally
  65.                 If objStream Is Nothing Then
  66.                     objStream.Close()
  67.                 End If
  68.             End Try
  69.         End Sub
  70.  
  71.         Private Shared Sub WriteStream(ByVal objResponse As HttpResponse, ByVal objStream As Stream)
  72.             Dim bytBuffer As Byte() = New Byte(9999) {}
  73.             Dim intLength As Integer
  74.             Dim lngDataToRead As Long
  75.             Try
  76.                 lngDataToRead = objStream.Length
  77.                 While lngDataToRead > 0
  78.                     If objResponse.IsClientConnected Then
  79.                         intLength = objStream.Read(bytBuffer, 0, 10000)
  80.                         objResponse.OutputStream.Write(bytBuffer, 0, intLength)
  81.                         objResponse.Flush()
  82.                         ' TODO: NotImplemented statement: ICSharpCode.SharpRefactory.Parser.AST.VB.ReDimStatement
  83.                         lngDataToRead = lngDataToRead - intLength
  84.                     Else
  85.                         lngDataToRead = -1
  86.                     End If
  87.                 End While
  88.             Catch ex As Exception
  89.                 objResponse.Write("Error : " & ex.Message)
  90.             Finally
  91.                 If objStream Is Nothing Then
  92.                     objStream.Close()
  93.                     objResponse.Flush()
  94.                 End If
  95.             End Try
  96.         End Sub
  97.  
  98.     End Class
  99. End Namespace


if i write refresh.redirect() line before download file, downloading without error but not refresh.



Can anybody give me solution for this problem

Thanks Advance
Mar 30 '09 #1
1 3495
Frinavale
9,735 Expert Mod 8TB
You have to keep the ASP page life cycle in mind when developing web applications.
  • A request is made for a page or resource
  • The Page Load occurs
  • The Event that caused the request occurs
  • The Page is sent to the browser
  • Everything is unloaded.

So, what's happening in your case is the user is requesting a file, the page loads file and then sends it as a response back to the page so that the file can be downloaded.

Since the data has been sent to the browser, how is the server supposed to tell the browser to redirect?

That's why you're getting the error.

You're going to have to look into having the page make another request back to the server once the file is finished downloading.

I have no idea how to do this.

Does the page have to be redirected After the file is downloaded? Or can it be redirected While the file is downloading?

-Frinny
Mar 31 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Dave | last post by:
Hey, I'm running a localhost web server...W2K with IIS 5.0, and I'm trying to use ASP/ADO to access my Access database. When I first load the page, everything works, but if i hit refresh (F5 or...
5
by: John Morgan | last post by:
I am using the following link to download a file of about 50k <a target= "_blank" href="http://www.bsecs.org.uk/ExecDocs/documentStore/elfridaWord.doc">open file</a> If I save the file to...
3
by: Tim::.. | last post by:
Can someone please tell my why I get the following problem when I type the following piece of code! How do I get around this??? The idea is that when a user clicks a button on a form it causes...
1
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
3
by: just.starting | last post by:
Hi, My dot net client downloads files and checks for any new files time to time. The server is apache2.0.53 server. So what happens is that my file download thing works fine if I dont try to call...
5
by: fniles | last post by:
We created an ActiveX control and marked it as safe for scripting using Implements IObjectSafety. We then created a CAB file and signed it using Verisign. We also created a license file (LPK file)...
102
by: hug | last post by:
www.webmaster, was suggested that this ng could be a better place.] I've updated my test server to handle if-modified-since. I've noticed that the (old copies I run of) IE and Netscape seem...
4
by: David | last post by:
I'm using the AxSHDocVw.WebBrowser control to download data from a webpage at work (it's an internal page on my company's intranet). The page produces a runtime error after a while and the...
22
by: V S Rawat | last post by:
(bringing the discussion here for php.general) I am on xpsp3, wampserver 2.0, having apache 2.2.8, php 5.2.6, MySQL 5.0.51b. http://localhost/ is E:\wamp\www\ I have put the first php script...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.