Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi all

 

I am downloading a .csv file will some data in it. The code that I am using is as follows:

 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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

Problem solved. Here is what I used:

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Posted

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.

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

  • 2 weeks later...
Posted (edited)

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).

 

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.

Edited by mike55

A Client refers to the person who incurs the development cost.

A Customer refers to the person that pays to use the product.

------

My software never has bugs. It just develops random features. (Mosabama vbforums.com)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...