CipherIS
asked on
C# - Downloading file adding extra data (unintentionally)
The problem I have is that I am downloading data via WCF but it is adding to the text file.
THE DATA SHOULD LOOKE LIKE BELOW
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
BUT I AM GETTING
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
Below is the code I am using.
Response.BufferOutput = false;
byte[] buffer = new byte[65000];
int bytesRead = 0;
HttpContext.Current.Respon se.Clear() ;
HttpContext.Current.Respon se.ClearHe aders();
HttpContext.Current.Respon se.Content Type = "application/octet-stream" ;
HttpContext.Current.Respon se.AddHead er("Conten t-Disposit ion",
"attachment; filename=" + requestData.FileName);
bytesRead = fileInfo.FileByteStream.Re ad(buffer, 0, buffer.Length);
while (bytesRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnecte d)
{
Response.OutputStream.Writ e(buffer, 0, bytesRead);
// Flush the data to the HTML output.
Response.Flush();
buffer = new byte[65000];
bytesRead = fileInfo.FileByteStream.Re ad(buffer, 0, buffer.Length);
}
else
{
bytesRead = -1;
}
}
Response.Flush();
How do I fix?
THE DATA SHOULD LOOKE LIKE BELOW
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
BUT I AM GETTING
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
Below is the code I am using.
Response.BufferOutput = false;
byte[] buffer = new byte[65000];
int bytesRead = 0;
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
HttpContext.Current.Respon
"attachment; filename=" + requestData.FileName);
bytesRead = fileInfo.FileByteStream.Re
while (bytesRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnecte
{
Response.OutputStream.Writ
// Flush the data to the HTML output.
Response.Flush();
buffer = new byte[65000];
bytesRead = fileInfo.FileByteStream.Re
}
else
{
bytesRead = -1;
}
}
Response.Flush();
How do I fix?
I don't quite understand what you are intending to return in the web service call...
ASKER
I am returning a text file. This is the data in the text file.
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
This is what is being returned in the text file
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
Some how this is being appended to the text file and it shouldn't be.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
This is what is being returned in the text file
Record Count 14866
"Field" "Type" "Length"
"oldparent" "Text" "8"
"NEWPARENT" "Text" "8"
"PID" "Text" "8"
"STATUS_BLD" "Text" "1"
"DATE" "Text" "8"
"CREASON" "Text" "1"
"MPID" "Text" "8"
"NAME" "Text" "30"
"STREET" "Text" "24"
"CITY" "Text" "16"
"STATE" "Text" "2"
"ZIPCODE" "Text" "7"
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
Some how this is being appended to the text file and it shouldn't be.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
What type of WCF service is this?
Instead of application/octet-stream, did you try text/plain?
Instead of application/octet-stream, did you try text/plain?
ASKER
I replaced with text/plain and still getting below added to it. Didn't work.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head><title>
Download
</title><link rel="icon" href="images/favicon.ico" type="image/ico" /><link href="Styles/Site.css" rel="stylesheet" type="text/css" />
<style type="text/css">
.style7
{
width: 42%;
}
</style>
ETC.....
ASKER
Another thing I need to mention is that the download downloads other file types such as:
PDF and XLS
PDF and XLS
What type of WCF web service are you working with?
ASKER
basicHTTPBinding.
The webservice works fine. It returns all the files. The only issue is when the text file is being downloaded it adds the HTML code to the text file.
Here is the code in the WCF Service that returns the file
public RemoteFileInfo APIDownloadFile(DownloadRe quest request)
{
string UNCPath = string.Empty;
RemoteFileInfo result = new RemoteFileInfo();
try
{
string WriteError = Path.Combine(@"C:\Logs\API Fulfillmen t", "APIDownloadFile.txt");
using (StreamWriter outfile2 = new StreamWriter(WriteError))
{
//Get Path and FileName
UNCPath = APIGetUNCPath(request.Acce ssKey, request.FileName);
outfile2.WriteLine(UNCPath + " | " + DateTime.Now, Environment.NewLine);
FileInfo fileInfo = new FileInfo(UNCPath);
outfile2.WriteLine("fileIn fo" + " | " + DateTime.Now);
//Check if exists
if (!fileInfo.Exists)
{
throw new System.IO.FileNotFoundExce ption("Fil e not found", request.FileName);
}
request.FileSize = fileInfo.Length;
outfile2.WriteLine("File Exists" + " | " + DateTime.Now, Environment.NewLine);
//Open stream
FileStream stream = new FileStream(UNCPath, FileMode.Open, FileAccess.Read);
outfile2.WriteLine("FileSt ream" + " | " + DateTime.Now, Environment.NewLine);
//Return result
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;
outfile2.WriteLine("Result " + " | " + DateTime.Now, Environment.NewLine);
//Close the stream to prevent file access denied issue
OperationContext.Current.O perationCo mpleted += (sender, args) =>
{
if (result.FileByteStream != null)
{
result.FileByteStream.Disp ose();
}
};
outfile2.WriteLine("Close" + " | " + DateTime.Now, Environment.NewLine);
outfile2.Close();
}
}
catch (Exception ex)
{
//Need to write Error someplace
string s = ex.Message;
string WriteError2 = Path.Combine(@"C:\Logs\API Fulfillmen t", "APIDownloadFile_Error.txt ");
using (StreamWriter outfile = new StreamWriter(WriteError2))
{
outfile.WriteLine(s + " | " + DateTime.Now, Environment.NewLine);
outfile.Close();
}
}
return result;
}
The webservice works fine. It returns all the files. The only issue is when the text file is being downloaded it adds the HTML code to the text file.
Here is the code in the WCF Service that returns the file
public RemoteFileInfo APIDownloadFile(DownloadRe
{
string UNCPath = string.Empty;
RemoteFileInfo result = new RemoteFileInfo();
try
{
string WriteError = Path.Combine(@"C:\Logs\API
using (StreamWriter outfile2 = new StreamWriter(WriteError))
{
//Get Path and FileName
UNCPath = APIGetUNCPath(request.Acce
outfile2.WriteLine(UNCPath
FileInfo fileInfo = new FileInfo(UNCPath);
outfile2.WriteLine("fileIn
//Check if exists
if (!fileInfo.Exists)
{
throw new System.IO.FileNotFoundExce
}
request.FileSize = fileInfo.Length;
outfile2.WriteLine("File Exists" + " | " + DateTime.Now, Environment.NewLine);
//Open stream
FileStream stream = new FileStream(UNCPath, FileMode.Open, FileAccess.Read);
outfile2.WriteLine("FileSt
//Return result
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;
outfile2.WriteLine("Result
//Close the stream to prevent file access denied issue
OperationContext.Current.O
{
if (result.FileByteStream != null)
{
result.FileByteStream.Disp
}
};
outfile2.WriteLine("Close"
outfile2.Close();
}
}
catch (Exception ex)
{
//Need to write Error someplace
string s = ex.Message;
string WriteError2 = Path.Combine(@"C:\Logs\API
using (StreamWriter outfile = new StreamWriter(WriteError2))
{
outfile.WriteLine(s + " | " + DateTime.Now, Environment.NewLine);
outfile.Close();
}
}
return result;
}
Have you thought about Streaming?
How to: Enable Streaming
http://msdn.microsoft.com/en-us/library/ms789010.aspx
How to: Enable Streaming
http://msdn.microsoft.com/en-us/library/ms789010.aspx
The problem does not look like with the service return but the way you are returning the file to the client. It looks like the Master Page/Layout gets rendered after you output the file.
I'm not sure about the best way to inhibit page rendering from a particular callback. Try calling Response.End right after you flush
Response.Flush();
Response.End();
I'm not sure about the best way to inhibit page rendering from a particular callback. Try calling Response.End right after you flush
Response.Flush();
Response.End();
ASKER
@ambience - Response.End() causes an error.
ASKER
Does anyone have an answer?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I figured it out.