|
 |
aspx thread: Problem while creating .txt file
Message #1 by Neeta.Ambekar@l... on Tue, 21 May 2002 14:30:02 +0530
|
|
Hi All,
I am creating a txt file from Asp.Net using streamwriter and file stream
and then giving option to user to download it on his machine. Code is as
below
Dim myFileStream As FileStream
Dim myFile As FileInfo
Dim myCopiedFile As FileInfo
Dim intCOunt As Integer
Dim intColCnt As Integer
Dim strWriteHeading As String
Dim strWriteBody As String
Dim dtsetSearch As DataSet
Dim strFileName As String
Dim strFilePath As String
Try
'dtsetFile is dataset contains data to be copied in file
dtsetSearch = Cache("dtsetFile")
strFileName = "SearchResult" & "usrId" & ".txt"
strFilePath = "C:\"
If File.Exists(Path:=strFilePath & strFileName) Then
File.Delete(Path:=strFilePath & strFileName)
End If
'create a new file using, set = to file stream object
myFileStream = File.Create(Path:=strFilePath & strFileName)
myFileStream.Close()
'get the newly created file
myFile = New FileInfo(filename:=strFilePath & strFileName)
Dim fileStream As FileStream
Dim streamWriter As StreamWriter
'create a new instance of the file stream object
'note: if the file does not exist, the constructor create it
fileStream = New FileStream(Path:=strFilePath & strFileName, _
mode:=FileMode.OpenOrCreate, access:=FileAccess.Write)
'create an instance of a character writer
streamWriter = New StreamWriter(Stream:=fileStream)
'set the file pointer to the end of the file
streamWriter.BaseStream.Seek(offset:=0, origin:=SeekOrigin.End)
'write a line of text to the end of the file
strWriteHeading = ""
For intCOunt = 0 To dtsetSearch.Tables(0).Rows.Count - 1
strWriteBody = ""
For intColCnt = 0 To
dtsetSearch.Tables(0).Columns.Count - 2
strWriteBody = strWriteBody & vbTab &
dtsetSearch.Tables(0).Rows(intCOunt).Item(intColCnt)
Next
streamWriter.WriteLine(value:=strWriteBody)
Next
End If
'apply the update to the file
streamWriter.Flush()
'close the stream writer
streamWriter.Close()
'close the file stream object
fileStream.Close()
streamWriter = Nothing
fileStream = Nothing
Response.Clear()
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", _
"attachment; filename=""" & strFileName & """")
Response.Flush()
Response.WriteFile(strFilePath & strFileName)
end sub
Problem is it is creating txt file with supplied data plus appended by
full HTML code
e.g
USER ID AGENT NAME LICENSE NUMBER SELECT
14 a11 a11 68554766 8
15 a12 a12 3
16 a13 a13 3
17 a21 a21 6584645 3
18 a22 a22 1
19 a31 a31 1
20 a32 a32 1
21 a23 a23 1
22 a32 a32 1
23 a33 a33 2
24 a42 a42 1
25 a44 a44 456546 4
27 a51 a51 2
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>AMSSearchAgent</title>
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5"
name="vs_targetSchema">
</HEAD>
<body bottomMargin="0" bgColor="#ffffff" leftMargin="0" topMargin="0"
rightMargin="0">
<form name="frmSearchAgent" method="post" action
="AMSSearchAgent.aspx?Operation=View" id="frmSearchAgent" enctype
="multipart/form-data">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" value="
Can anyone suggest me any way to remove this appended HTMl code ....
Regards
Neeta
Message #2 by Imar Spaanjaars <Imar@S...> on Tue, 21 May 2002 18:41:22 +0200
|
|
Are you buffering the page? If buffering is disabled, the contents for the
page have already been sent, so clearing the buffer won't help you.
Add the following statement to your page directive: buffer="True".
Buffering is on by default, but maybe your IIS settings overrule that.
You also may need to stop the Response (Response.End) to avoid content
after your code from being displayed.
HtH
Imar
At 02:30 PM 5/21/2002 +0530, you wrote:
>Hi All,
>
>I am creating a txt file from Asp.Net using streamwriter and file stream
>and then giving option to user to download it on his machine. Code is as
>below
>
>Dim myFileStream As FileStream
>Dim myFile As FileInfo
>Dim myCopiedFile As FileInfo
> Dim intCOunt As Integer
>Dim intColCnt As Integer
> Dim strWriteHeading As String
> Dim strWriteBody As String
> Dim dtsetSearch As DataSet
> Dim strFileName As String
> Dim strFilePath As String
> Try
> 'dtsetFile is dataset contains data to be copied in file
> dtsetSearch = Cache("dtsetFile")
> strFileName = "SearchResult" & "usrId" & ".txt"
> strFilePath = "C:\"
> If File.Exists(Path:=strFilePath & strFileName) Then
> File.Delete(Path:=strFilePath & strFileName)
> End If
> 'create a new file using, set = to file stream object
> myFileStream = File.Create(Path:=strFilePath & strFileName)
> myFileStream.Close()
>
> 'get the newly created file
> myFile = New FileInfo(filename:=strFilePath & strFileName)
>
>
> Dim fileStream As FileStream
> Dim streamWriter As StreamWriter
>
> 'create a new instance of the file stream object
> 'note: if the file does not exist, the constructor create it
> fileStream = New FileStream(Path:=strFilePath & strFileName, _
> mode:=FileMode.OpenOrCreate, access:=FileAccess.Write)
>
> 'create an instance of a character writer
> streamWriter = New StreamWriter(Stream:=fileStream)
>
> 'set the file pointer to the end of the file
> streamWriter.BaseStream.Seek(offset:=0, origin:=SeekOrigin.End)
>
> 'write a line of text to the end of the file
> strWriteHeading = ""
>
> For intCOunt = 0 To dtsetSearch.Tables(0).Rows.Count - 1
> strWriteBody = ""
> For intColCnt = 0 To
>dtsetSearch.Tables(0).Columns.Count - 2
> strWriteBody = strWriteBody & vbTab &
>dtsetSearch.Tables(0).Rows(intCOunt).Item(intColCnt)
> Next
> streamWriter.WriteLine(value:=strWriteBody)
> Next
> End If
>
> 'apply the update to the file
> streamWriter.Flush()
>
> 'close the stream writer
> streamWriter.Close()
> 'close the file stream object
> fileStream.Close()
>
> streamWriter = Nothing
> fileStream = Nothing
>
> Response.Clear()
> Response.ContentType = "application/octet-stream"
> Response.AddHeader("Content-Disposition", _
> "attachment; filename=""" & strFileName & """")
> Response.Flush()
> Response.WriteFile(strFilePath & strFileName)
>
>end sub
>
>Problem is it is creating txt file with supplied data plus appended by
>full HTML code
>
>e.g
> USER ID AGENT NAME LICENSE NUMBER SELECT
> 14 a11 a11 68554766 8
> 15 a12 a12 3
> 16 a13 a13 3
> 17 a21 a21 6584645 3
> 18 a22 a22 1
> 19 a31 a31 1
> 20 a32 a32 1
> 21 a23 a23 1
> 22 a32 a32 1
> 23 a33 a33 2
> 24 a42 a42 1
> 25 a44 a44 456546 4
> 27 a51 a51 2
>
><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
Message #3 by "Mingkun Goh" <mangokun@h...> on Wed, 22 May 2002 07:30:45
|
|
I created a new asp.net page and put the code below in the Page_Load event.
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Response.ContentType = "text/plain"
Response.Output.WriteLine("mango")
Response.Output.Close()
'Dim s As New StreamWriter(Response.OutputStream)
's.WriteLine("mango")
's.Close()
End Sub
When I open this page in my browser, I am prompted with a 'File Download'
dialog, which says:
WebForm1.aspx from localhost
Would you like to open the file or save it to your computer?
You can remove the line 'Response.ContentType = "text/plain"' if you want
to display the Text in the browser instead of it being a text file.
You can also use the commented-out code instead of the Response.Output ...
lines.
Previous message:
> Hi All,
I am creating a txt file from Asp.Net using streamwriter and file stream
and then giving option to user to download it on his machine. Code is as
below
...
Can anyone suggest me any way to remove this appended HTMl code ....
Regards
Neeta
|
|
 |