Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP.NET 2.0/C# Response to client is masterpage instead of file.

Reply
Thread Tools

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

 
 
Michael D. Ober
Guest
Posts: n/a
 
      07-31-2007
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);
}
}


 
Reply With Quote
 
 
 
 
Michael D. Ober
Guest
Posts: n/a
 
      08-01-2007
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.nospam> wrote in message
news:(E-Mail Removed)...
> 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);
> }
> }
>



 
Reply With Quote
 
 
 
 
Michael D. Ober
Guest
Posts: n/a
 
      08-01-2007
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.nospam> wrote in message
news:%(E-Mail Removed)...
> 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.nospam> wrote in message
> news:(E-Mail Removed)...
>> 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);
>> }
>> }
>>

>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Changing a control on a masterpage that uses a second masterpage Torben Laursen ASP .Net 4 03-15-2008 03:27 AM
Masterpage change its masterpage mr t ASP .Net 0 08-29-2007 10:11 PM
MasterPages - take plain form, add MasterPage. MasterPage does NOT appear when run Randy Smith ASP .Net 9 04-14-2007 09:06 AM
MasterPage inheriting a MasterPage... is it possible? ThunderMusic ASP .Net 2 09-27-2006 07:57 PM
MasterPage -> Page -> MasterPage Code Access =?Utf-8?B?QWxleCBNYWdoZW4=?= ASP .Net 3 04-14-2006 12:34 PM



Advertisments