Response.Transmit with csv/excel file not working

J

Julia B

Hi

I've got an application where users can select some files to view in the
browser. The files are either .doc or .csv files.
The .doc files open in the browser in Word, but I get an error if I try and
open the .csv file using Excel - the error is that both the contents of the
file and the contents of the current webpage are displayed in the Excel
window. It's very strange. I just can't get the .csv file to open correctly.
Anyone any clues? Here's the code:

Private Sub SearchGrid_RowCommand(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewCommandEventArgs) Handles
SearchGrid.RowCommand
Dim index As Integer
Dim selectedRow As GridViewRow
Dim filePath As String
If e.CommandName = "OrigRecord" Or e.CommandName = "OrigReport" Then
'if the user has selected to open the original record
'get the selected row
index = Convert.ToInt32(e.CommandArgument)
selectedRow = Me.SearchGrid.Rows(index)
Response.Clear()
Response.ClearHeaders()
'now get the record name
If e.CommandName = "OrigRecord" Then
filePath = selectedRow.Cells(9).Text
filePath = Global.Global_asax.calRecordFilePath & filePath
Response.AppendHeader("Content-Disposition",
"attachment;filename=" & filePath)
Response.ContentType = "application/vnd.ms-excel"
Else
filePath = selectedRow.Cells(10).Text
filePath = Global.Global_asax.calReportFilePath & filePath
Response.AppendHeader("Content-Disposition",
"attachment;filename=" & filePath)
Response.ContentType = "application/msword"
End If
'now we've got the full path for the file, open it
Response.TransmitFile(filePath)
Response.Buffer = False
Else
'do nothing
End If
End Sub

Note: the user selects the file open option in a gridview from a command
button, if that makes any difference and the gridview is in a content page in
a master page.

Thanks in advance.
Julia
 
A

Andrew Morton

Julia said:
I've got an application where users can select some files to view in
the browser. The files are either .doc or .csv files.
The .doc files open in the browser in Word, but I get an error if I
try and open the .csv file using Excel - the error is that both the
contents of the file and the contents of the current webpage are
displayed in the Excel window. It's very strange. I just can't get
the .csv file to open correctly. Anyone any clues? Here's the code:
Response.ContentType = "application/vnd.ms-excel"

A couple of ideas...

Seeing as a csv is actually text/plain, that may be the problem.

Or perhaps you could change the file extension to xls.

Andrew
 
J

Jeff Johnson

Response.AppendHeader("Content-Disposition",
"attachment;filename=" & filePath)

Content-Disposition headers should only contain file names, not full paths.
In the first place, the user will save this file wherever he wants to; you
can't control it, and in the second place, it's a potential (albeit small)
security risk to expose your local paths to a client. So wherever you're
setting this header, you should be using
System.IO.Path.GetFileName(filePath).

As to the ultimate problem, like Andrew said, CSV files are NOT Excel files,
even though Excel really really wants you to believe they are. text/csv is a
registered MIME type (I looked it up), so I suggest giving that a try.
 
J

Julia B

Thanks to both of you, however the problem is not solved.

I changed the file path, that's a good tip, I really do not want to expose
the file path to my users. I also changed the mime type as suggested.

The file does open, but it still includes the html for the contents of the
page that the link originated from. Very strange..... any ideas?

Julia
 
J

Julia B

Got the solution - I needed to add Response.End() after the
Response.Transmit() to stop unecessary information being sent.

Thanks
Julia
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top