Issue with opening a file that I am downloading.

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi all

I am downloading a .csv file will some data in it. The code that I am using is as follows:
Code:
  Private Sub DownloadFile(ByVal filePath As String, ByVal fileName As String)
        Dim fs As FileStream


        ' Open the file.
        fs = File.Open(filePath, FileMode.OpenOrCreate)
        Dim byBytes(Convert.ToInt32(fs.Length)) As Byte
        fs.Read(byBytes, 0, Convert.ToInt32(fs.Length))
        fs.Close()

        ' Delete the file from the system.
        If Not fileName = "SampleCSV.csv" Then
            File.Delete(filePath)
        End If
        Response.AddHeader("Content-disposition", "attachment; fileName=" + fileName)
        Response.ContentType = "application/octet-stream"
        Response.BinaryWrite(byBytes)
        Response.End()

The issue that I am experiencing is that when the user clicks the "Open" button, I get the following error message:
'C:\Documents and Settings\user\Local settings\Temporary Internet Files\Content.IE5\0t6lg1s3\SampleCSV[1].csv' Could not be found. Check the spelling of the file name, and verify that the location is correct. If you are trying to open the file from your list of most recently used files on the File menu, make sure that the file has not been renamed, moved, or deleted.

Any suggestions? Should I be employing a different means of downloading the file?

Mike55.
 
Problem solved. Here is what I used:
Code:
FileStream liveStream = new FileStream(localfilename, FileMode.Open,
                                       FileAccess.Read);

byte[] buffer = new byte[(int)liveStream.Length];
liveStream.Read(buffer, 0, (int)liveStream.Length);
liveStream.Close();

Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Length", buffer.Length.ToString());
Response.AddHeader("Content-Disposition", "attachment; filename=" +
                   originalFilename);
Response.BinaryWrite(buffer);
Response.End();

Mike55.
 
Ok, I am after deploying my code from the test server to the real server. On the test server, all of the above worked correctly. When I click the download button, I get the Open Save dialog box. However, when I click on the open option, my file is opened in the existing web page.

Any suggestions why this is occuring?

Mike55.
 
I am still having the problem with the file being opened on a web browser window. I am also have the problem in that if I try to save my file I get an error message telling me that the file cannot be found. Now I have found the following code, and I have tested it on the real web server, as opposed to the test web server. The file in question opens and can be saved correctly. The issue that I am experiencing is that there is code information regarding the web page also included in the file (See attachment).

Code:
Dim root As String = "C:\Suretxtlog\Templates\"

        Dim filePath As String = "C:\Suretxtlog\Templates\SampleCSV.csv"

        If Not filePath Is Nothing Then
            If System.IO.File.Exists(filePath) And filePath.StartsWith(root) Then
                Dim filename As String = System.IO.Path.GetFileName(filePath)
                Response.Clear()
                Response.ContentType = "Application/x-msexcel"
                Response.AddHeader("Content-Disposition", "attachment; filename=" & filename & ".csv")
                Response.Flush()
                Response.WriteFile(filePath)
            End If
        End If
Any suggestions on how I can prevent this additional information being included in the file.

Mike55.
 
Last edited:
Back
Top