473,394 Members | 1,726 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,394 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 2610
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...
2
by: Buddy Ackerman | last post by:
I am creating a ZIP file on the fly and streaming it out to the client using Response.WriteFile(FileName, StartPos, FileSize). Because this ZIP contains files that are on my server and I don't...
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
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.