473,230 Members | 1,435 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,230 software developers and data experts.

ASP.NET 2.0/C# Response to client is masterpage instead of file.

When I single step through the code below, it sends back the PDF file that
is retrieved in the line fm.GetAccountPDF(...). When I run without single
stepping, I get the master page for this page. I have actually saved the
file returned by IE 7 as a text file and opened it in notepad, so I have
confirmed that it is indeed the page master being returned. The code is in
the code behind file for the aspx page and is called directly from a link on
the page. In all cases, the PDF file is correctly created on the server and
I can open it directly from Windows Explorer. This code is actually
slightly modified from the code in MS TechNet article 812406
http://support.microsoft.com/kb/812406/en-us.

What am I doing wrong?

Thanks,
Mike Ober.
protected void StreamFile(object sender, EventArgs e)
{
const int ChunkSize = 8192; // Use a cluster size multiple for performance
FileManager fm = null;
System.IO.Stream iStream = null;
try {
fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
try {
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);
// Total bytes to read:
long dataToRead = fiPDF.Length;
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" +
fiPDF.Name);
Response.AppendHeader("Content-Length", dataToRead.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF);
// Now write the file
// Buffer to read sytes in chunks:
byte[] buffer;
// Length of the file:
int length;
// Open the file.
iStream = new System.IO.FileStream(fiPDF.FullName,
System.IO.FileMode.Open,
System.IO.FileAccess.Read,
System.IO.FileShare.Read);
// Read the bytes.
while (dataToRead 0) {
// Verify that the client is connected.
if (Response.IsClientConnected) {
// Read the data in buffer.
buffer = new byte[ChunkSize];
length = iStream.Read(buffer, 0, ChunkSize);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
// Update our position
dataToRead = dataToRead - length;
} else {
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
Response.End();

} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message;

} finally {
if (iStream != null) {
iStream.Close();
}
}

} catch {
Response.Redirect(SingularityLibrary.config.CoreCo nfiguration.appPath(Request).ToString()
+ "/" + Wakefield.Core.Config.WakefieldConfig.ClientLoginP age);
}
}
Jul 31 '07 #1
2 3159
Updated information - the content I get back is the HTML (up to the length
of the file size) of the page requesting the file. Here's the new code that
uses the Response.TransmitFile() function.

protected void StreamFile(object sender, EventArgs e)
{
System.IO.Stream iStream = null;
try {
FileManager fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fiPDF.Name);
Response.AddHeader("Content-Length", fiPDF.Length.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF);
Response.TransmitFile(fiPDF.FullName);

} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message;
} finally {
if (iStream != null) {
iStream.Close();
}
}
}

Mike.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:O4*************@TK2MSFTNGP06.phx.gbl...
When I single step through the code below, it sends back the PDF file that
is retrieved in the line fm.GetAccountPDF(...). When I run without single
stepping, I get the master page for this page. I have actually saved the
file returned by IE 7 as a text file and opened it in notepad, so I have
confirmed that it is indeed the page master being returned. The code is
in the code behind file for the aspx page and is called directly from a
link on the page. In all cases, the PDF file is correctly created on the
server and I can open it directly from Windows Explorer. This code is
actually slightly modified from the code in MS TechNet article 812406
http://support.microsoft.com/kb/812406/en-us.

What am I doing wrong?

Thanks,
Mike Ober.
protected void StreamFile(object sender, EventArgs e)
{
const int ChunkSize = 8192; // Use a cluster size multiple for
performance
FileManager fm = null;
System.IO.Stream iStream = null;
try {
fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
try {
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);
// Total bytes to read:
long dataToRead = fiPDF.Length;
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename="
+ fiPDF.Name);
Response.AppendHeader("Content-Length", dataToRead.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF);
// Now write the file
// Buffer to read sytes in chunks:
byte[] buffer;
// Length of the file:
int length;
// Open the file.
iStream = new System.IO.FileStream(fiPDF.FullName,

System.IO.FileMode.Open,

System.IO.FileAccess.Read,

System.IO.FileShare.Read);
// Read the bytes.
while (dataToRead 0) {
// Verify that the client is connected.
if (Response.IsClientConnected) {
// Read the data in buffer.
buffer = new byte[ChunkSize];
length = iStream.Read(buffer, 0, ChunkSize);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
// Update our position
dataToRead = dataToRead - length;
} else {
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
Response.End();

} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message;

} finally {
if (iStream != null) {
iStream.Close();
}
}

} catch {

Response.Redirect(SingularityLibrary.config.CoreCo nfiguration.appPath(Request).ToString()
+ "/" + Wakefield.Core.Config.WakefieldConfig.ClientLoginP age);
}
}

Aug 1 '07 #2
The problem I was having was that the PDF file was being created by a VMS
server, writing to a Windows Storage Server via NFS, and then not being
available for the open or TransmitFile functions. Solution involved
creating a loop that would try to open the file for read in exclusive mode.
If the open failed, sleep for a second and try again. What a kludge.
Here's the code:

protected void StreamFile(object sender, EventArgs e)
{
hgc_outputMsg.InnerHtml = "";
try {
FileManager fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);
System.IO.FileStream TestForLock = null;
while (TestForLock == null) {
try {
TestForLock = new System.IO.FileStream(fiPDF.FullName,

System.IO.FileMode.Open,

System.IO.FileAccess.Read,

System.IO.FileShare.None);
} catch {
System.Threading.Thread.Sleep(new System.TimeSpan(0,0,1));
}
}
if (TestForLock != null)
TestForLock.Close();

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fiPDF.Name);
Response.AddHeader("Content-Length", fiPDF.Length.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF);
Response.TransmitFile(fiPDF.FullName);
} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message + "<BR />";
}
}

I also discovered that you don't need the Content-Length header when using
Transmit File, at least with IE 7, but left it in for other browsers.

Mike.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
Updated information - the content I get back is the HTML (up to the length
of the file size) of the page requesting the file. Here's the new code
that uses the Response.TransmitFile() function.

protected void StreamFile(object sender, EventArgs e)
{
System.IO.Stream iStream = null;
try {
FileManager fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" +
fiPDF.Name);
Response.AddHeader("Content-Length", fiPDF.Length.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF);
Response.TransmitFile(fiPDF.FullName);

} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message;
} finally {
if (iStream != null) {
iStream.Close();
}
}
}

Mike.

"Michael D. Ober" <obermd.@.alum.mit.edu.nospamwrote in message
news:O4*************@TK2MSFTNGP06.phx.gbl...
>When I single step through the code below, it sends back the PDF file
that is retrieved in the line fm.GetAccountPDF(...). When I run without
single stepping, I get the master page for this page. I have actually
saved the file returned by IE 7 as a text file and opened it in notepad,
so I have confirmed that it is indeed the page master being returned.
The code is in the code behind file for the aspx page and is called
directly from a link on the page. In all cases, the PDF file is
correctly created on the server and I can open it directly from Windows
Explorer. This code is actually slightly modified from the code in MS
TechNet article 812406 http://support.microsoft.com/kb/812406/en-us.

What am I doing wrong?

Thanks,
Mike Ober.
protected void StreamFile(object sender, EventArgs e)
{
const int ChunkSize = 8192; // Use a cluster size multiple for
performance
FileManager fm = null;
System.IO.Stream iStream = null;
try {
fm = new FileManager((ASPNET2VAX.Core)Session["vax"]);
try {
System.IO.FileInfo fiPDF =
fm.GetAccountPDF(Request.QueryString["id"]);
// Total bytes to read:
long dataToRead = fiPDF.Length;
Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename="
+ fiPDF.Name);
Response.AppendHeader("Content-Length", dataToRead.ToString());
Response.ContentType =
ASPNET2VAX.SupportRoutines.LookupMIMEType(fiPDF );
// Now write the file
// Buffer to read sytes in chunks:
byte[] buffer;
// Length of the file:
int length;
// Open the file.
iStream = new System.IO.FileStream(fiPDF.FullName,

System.IO.FileMode.Open,

System.IO.FileAccess.Read,

System.IO.FileShare.Read);
// Read the bytes.
while (dataToRead 0) {
// Verify that the client is connected.
if (Response.IsClientConnected) {
// Read the data in buffer.
buffer = new byte[ChunkSize];
length = iStream.Read(buffer, 0, ChunkSize);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);
// Flush the data to the HTML output.
Response.Flush();
// Update our position
dataToRead = dataToRead - length;
} else {
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
Response.End();

} catch (Exception ex) {
hgc_outputMsg.InnerHtml += ex.Message;

} finally {
if (iStream != null) {
iStream.Close();
}
}

} catch {

Response.Redirect(SingularityLibrary.config.CoreC onfiguration.appPath(Request).ToString()
+ "/" + Wakefield.Core.Config.WakefieldConfig.ClientLoginP age);
}
}


Aug 1 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Analyst | last post by:
Is it possible to install DB2 CAE v5.2 using a response file ? From one of IBMs tech article, I found that the IBM DB2 Universal DB can be installed using a response file. My objective it to...
3
by: Keith Patrick | last post by:
I'd like to get some semblance of SmartNavigation-like behavior in a MasterPage-based content navigation app (primarily the lack of "flicker") I had it working in a version that used a MultiView...
9
by: User | last post by:
OK, I asked in a reply, but I've wasted far too long so I'm going to put a new post out there in hopes that it will be more visible. I have a MasterPage. I want to call a method declared in that...
2
by: jimmyjoe | last post by:
Hello, I have content that fits into a contentplaceholder on the MasterPage. I need to create a pop-up window from the content page. Whenever I use window.open it opens the content in the...
3
by: Andy | last post by:
I have an ASP.NET webpage that allows a user to upload a file from the client browser. I want to display an animated gif and report on the progress of the upload. The <INPUTtag used for uploading...
2
by: Michael D. Ober | last post by:
When I single step through the code below, it sends back the PDF file that is retrieved in the line fm.GetAccountPDF(...). When I run without single stepping, I get the master page for this page. ...
4
by: Sagaert Johan | last post by:
Hi I have a page based on a masterpage: when i use Response.Write then the markup appears in the masterpage content location and not in the content of the page itself (not in the location of...
2
by: boole | last post by:
Hi there, when my ASP page receives the client request, I want to gather the request data (form), promptly end the response to the client (successful) and continue doing what I need to do with the...
5
by: Max2006 | last post by:
Hi, I am trying to limit my wcf service endpoint to response to only given windows user or group. How can I do that? Is there any way to configure that in the .config file? Thank you, Max
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.