Click here to Skip to main content
15,896,557 members

Download file from a directory

Member 9291223 asked:

Open original thread
I have ran into a situation where im able to download a file by specifying the file path but the issue is that the file is download in a unknown format. The code below shows the downloading process:

if (e.CommandName == "ViewFiles")
{

int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = gvunclassified.Rows[index];

string division = row.Cells[4].Text;

string filenames = row.Cells[1].Text;

string paths = @"NewFolder1" + "\\" + division + "\\" + "UnClassified" + "\\" + filenames;
string drive = @"C:\Website2\RecordsManagement";
string temppath = Path.Combine(drive, paths);


// The file path to download.

string filepath = temppath;

// The file name used to save the file to the client's system..

string filename = Path.GetFileName(filepath);
System.IO.Stream stream = null;
try
{
// Open the file into a stream.
stream = new FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
// Total bytes to read:
long bytesToRead = stream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
// Read the bytes from the stream in small portions.
while (bytesToRead > 0)
{
// Make sure the client is still connected.
if (Response.IsClientConnected)
{
// Read the data into the buffer and write into the
// output stream.
byte[] buffer = new Byte[10000];
int length = stream.Read(buffer, 0, 10000);
Response.OutputStream.Write(buffer, 0, length);
Response.Flush();
// We have already read some bytes.. need to read
// only the remaining.
bytesToRead = bytesToRead - length;
}
else
{
// Get out of the loop, if user is not connected anymore..
bytesToRead = -1;
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
// An error occurred..
}
finally
{
if (stream != null)
{
stream.Close();
}
}

Any suggestion would be highly appreciated. Actually the most surprising part is that if I use google chrome as my browser then the file is downloaded in the right format but an unknown format file is download if I use IE or Mozilla firefox.
Tags: C# (C# 3.0)

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900