Hi! everybody,
I'm facing a quite strange download problem. I use the following code to
download an XML file to client side:
With Response
' clear buffer
Call .Clear()
' specify the type of the downloadable file
.ContentType = "application/octet-stream" ' I have
also tried to change this statement to Response.ContentType = "Text/XML"
, but the result is same
' det the default filename in the FileDownload dialog box
Call .AddHeader("Content-Disposition", "attachment;
filename=""Download.XML""")
' force to flush buffer
Call .Flush()
' download the file
Call .WriteFile("Original.XML")
End With
When the download is finished, I found that some HTML code are attached to
the end of my original XML statements in download file. The HTML code is
what exact the form just before download. I would like to know what causes
this result and the solution. Certainly, I need the XML content only.
Thanks for your attention and kindly advice!
Regards,
JamesOne suggestion, try Response.Write(file) instead of writefile, that may cure
the ailment.
--
----
Got TidBits?
Get it here: www.networkip.net/tidbits
"James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
news:ezSMNkYrDHA.2600@.TK2MSFTNGP09.phx.gbl...
> Hi! everybody,
> I'm facing a quite strange download problem. I use the following code to
> download an XML file to client side:
> With Response
> ' clear buffer
> Call .Clear()
> ' specify the type of the downloadable file
> .ContentType = "application/octet-stream" ' I have
> also tried to change this statement to Response.ContentType = "Text/XML"
> , but the result is same
> ' det the default filename in the FileDownload dialog box
> Call .AddHeader("Content-Disposition", "attachment;
> filename=""Download.XML""")
> ' force to flush buffer
> Call .Flush()
> ' download the file
> Call .WriteFile("Original.XML")
> End With
> When the download is finished, I found that some HTML code are attached to
> the end of my original XML statements in download file. The HTML code is
> what exact the form just before download. I would like to know what
causes
> this result and the solution. Certainly, I need the XML content only.
> Thanks for your attention and kindly advice!
> Regards,
> James
Alvin,
Thanks for your response. But even I change it to Response.Write, the
strange behavior still be there.
Regards,
James
"Alvin Bruney" <vapordan_spam_me_not@.hotmail_no_spamhotmail.com> bl
news:ebZTV6YrDHA.2416@.TK2MSFTNGP10.phx.gbl g...
> One suggestion, try Response.Write(file) instead of writefile, that may
cure
> the ailment.
Hello James,
Thanks for posting in the group.
Please try adding Call.End() before "End With" to see if it works.
I used the following code and it works great for me:
string filename = "C:\MyFolder\MyFile.xml";
System.IO.FileInfo file = new System.IO.FileInfo(fileName);
Response.Clear(); // clear the current output content from the buffer
Response.AppendHeader("Content-Disposition", "attachment; filename=" +
file.Name);
Response.AppendHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
Does that answer your question? If the problem is still there, please feel
free to post here.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
After call Call .WriteFile("Original.XML") type
Response.End();
Will work.
Thanks,
sswalia
MCSD, MCAD, OCA
"James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
news:ezSMNkYrDHA.2600@.TK2MSFTNGP09.phx.gbl...
> Hi! everybody,
> I'm facing a quite strange download problem. I use the following code to
> download an XML file to client side:
> With Response
> ' clear buffer
> Call .Clear()
> ' specify the type of the downloadable file
> .ContentType = "application/octet-stream" ' I have
> also tried to change this statement to Response.ContentType = "Text/XML"
> , but the result is same
> ' det the default filename in the FileDownload dialog box
> Call .AddHeader("Content-Disposition", "attachment;
> filename=""Download.XML""")
> ' force to flush buffer
> Call .Flush()
> ' download the file
> Call .WriteFile("Original.XML")
> End With
> When the download is finished, I found that some HTML code are attached to
> the end of my original XML statements in download file. The HTML code is
> what exact the form just before download. I would like to know what
causes
> this result and the solution. Certainly, I need the XML content only.
> Thanks for your attention and kindly advice!
> Regards,
> James
Hi! Yanhong and SSW,
Thanks for your reply! I works by adding Call .End() and solve my problem.
However, there is a side-effect by doing so. Threading.ThreadAbortException
is fired once the .End() method is called. My workround is to handle this
exception and put all remaining statements after the .End() method under
this exception handling routine. Do you have a better idea to overcome this
side-effect?
Anyway, thanks for your effort!
Regards,
James
"Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
news:PFz0azZrDHA.1544@.cpmsftngxa06.phx.gbl g...
> Hello James,
> Thanks for posting in the group.
> Please try adding Call.End() before "End With" to see if it works.
> I used the following code and it works great for me:
> string filename = "C:\MyFolder\MyFile.xml";
> System.IO.FileInfo file = new System.IO.FileInfo(fileName);
> Response.Clear(); // clear the current output content from the buffer
> Response.AppendHeader("Content-Disposition", "attachment; filename=" +
> file.Name);
> Response.AppendHeader("Content-Length", file.Length.ToString());
> Response.ContentType = "application/octet-stream";
> Response.WriteFile(file.FullName);
> Response.End();
> Does that answer your question? If the problem is still there, please feel
> free to post here.
> Best regards,
> Yanhong Huang
> Microsoft Online Partner Support
> Get Secure! C www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no
rights.
Hello
Put the remaining state after the .End() statement. The .NET Framework will
handle the ThreadAbortException
Best regards
Sherif
"James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
news:#mepTgarDHA.1488@.TK2MSFTNGP12.phx.gbl...
> Hi! Yanhong and SSW,
> Thanks for your reply! I works by adding Call .End() and solve my
problem.
> However, there is a side-effect by doing so.
Threading.ThreadAbortException
> is fired once the .End() method is called. My workround is to handle this
> exception and put all remaining statements after the .End() method under
> this exception handling routine. Do you have a better idea to overcome
this
> side-effect?
> Anyway, thanks for your effort!
> Regards,
> James
> "Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
> news:PFz0azZrDHA.1544@.cpmsftngxa06.phx.gbl g...
> > Hello James,
> > Thanks for posting in the group.
> > Please try adding Call.End() before "End With" to see if it works.
> > I used the following code and it works great for me:
> > string filename = "C:\MyFolder\MyFile.xml";
> > System.IO.FileInfo file = new System.IO.FileInfo(fileName);
> > Response.Clear(); // clear the current output content from the buffer
> > Response.AppendHeader("Content-Disposition", "attachment; filename=" +
> > file.Name);
> > Response.AppendHeader("Content-Length", file.Length.ToString());
> > Response.ContentType = "application/octet-stream";
> > Response.WriteFile(file.FullName);
> > Response.End();
> > Does that answer your question? If the problem is still there, please
feel
> > free to post here.
> > Best regards,
> > Yanhong Huang
> > Microsoft Online Partner Support
> > Get Secure! C www.microsoft.com/security
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
Hello, Sherif,
I have my own exceptional handler which handles all unexpected exceptions.
So if I don't handle it, this exception will fire my default exceptional
handling routine which logs exception details in my self-defined log and
also sends an e-mail to me. That's why I have to handle it. Do you think
there is other better workaround?
Regards,
James
"Sherif ElMetainy" <elmeteny.NOSPAM@.wayout.net.NOSPAM> bl
news:es0vEfbrDHA.1408@.TK2MSFTNGP11.phx.gbl g...
> Hello
> Put the remaining state after the .End() statement. The .NET Framework
will
> handle the ThreadAbortException
> Best regards
> Sherif
> "James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
> news:#mepTgarDHA.1488@.TK2MSFTNGP12.phx.gbl...
> > Hi! Yanhong and SSW,
> > Thanks for your reply! I works by adding Call .End() and solve my
> problem.
> > However, there is a side-effect by doing so.
> Threading.ThreadAbortException
> > is fired once the .End() method is called. My workround is to handle
this
> > exception and put all remaining statements after the .End() method under
> > this exception handling routine. Do you have a better idea to overcome
> this
> > side-effect?
> > Anyway, thanks for your effort!
> > Regards,
> > James
> > "Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
> > news:PFz0azZrDHA.1544@.cpmsftngxa06.phx.gbl g...
> > > Hello James,
> > > > Thanks for posting in the group.
> > > > Please try adding Call.End() before "End With" to see if it works.
> > > > I used the following code and it works great for me:
> > > > string filename = "C:\MyFolder\MyFile.xml";
> > > System.IO.FileInfo file = new System.IO.FileInfo(fileName);
> > > Response.Clear(); // clear the current output content from the buffer
> > > Response.AppendHeader("Content-Disposition", "attachment; filename=" +
> > > file.Name);
> > > Response.AppendHeader("Content-Length", file.Length.ToString());
> > > Response.ContentType = "application/octet-stream";
> > > Response.WriteFile(file.FullName);
> > > Response.End();
> > > > Does that answer your question? If the problem is still there, please
> feel
> > > free to post here.
> > > > Best regards,
> > > Yanhong Huang
> > > Microsoft Online Partner Support
> > > > Get Secure! C www.microsoft.com/security
> > > This posting is provided "AS IS" with no warranties, and confers no
> > rights.
>
Hello
ThreadAbortException is special in that even if your handler catches it, it
will be rethrown again at the end of the catch block, unless you call
Thread.ResetAbort(). In your case it is not appropriate to call
ResetAbort(), because this means the rendering of the page will continue.
If you don't want to call Response.End() which throws the
ThreadAbortException, you can override the Render Method and not call the
base class Render method.
Best regards
Sherif
"James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
news:eJqBmzbrDHA.3732@.tk2msftngp13.phx.gbl...
> Hello, Sherif,
> I have my own exceptional handler which handles all unexpected exceptions.
> So if I don't handle it, this exception will fire my default exceptional
> handling routine which logs exception details in my self-defined log and
> also sends an e-mail to me. That's why I have to handle it. Do you think
> there is other better workaround?
> Regards,
> James
> "Sherif ElMetainy" <elmeteny.NOSPAM@.wayout.net.NOSPAM> bl
> news:es0vEfbrDHA.1408@.TK2MSFTNGP11.phx.gbl g...
> > Hello
> > Put the remaining state after the .End() statement. The .NET Framework
> will
> > handle the ThreadAbortException
> > Best regards
> > Sherif
> > "James Wong" <cp_msdn@.commercialpress.com.hk.NO_SPAM> wrote in message
> > news:#mepTgarDHA.1488@.TK2MSFTNGP12.phx.gbl...
> > > Hi! Yanhong and SSW,
> > > > Thanks for your reply! I works by adding Call .End() and solve my
> > problem.
> > > > However, there is a side-effect by doing so.
> > Threading.ThreadAbortException
> > > is fired once the .End() method is called. My workround is to handle
> this
> > > exception and put all remaining statements after the .End() method
under
> > > this exception handling routine. Do you have a better idea to
overcome
> > this
> > > side-effect?
> > > > Anyway, thanks for your effort!
> > > > Regards,
> > > James
> > > > "Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
> > > news:PFz0azZrDHA.1544@.cpmsftngxa06.phx.gbl g...
> > > > Hello James,
> > > > > > Thanks for posting in the group.
> > > > > > Please try adding Call.End() before "End With" to see if it works.
> > > > > > I used the following code and it works great for me:
> > > > > > string filename = "C:\MyFolder\MyFile.xml";
> > > > System.IO.FileInfo file = new System.IO.FileInfo(fileName);
> > > > Response.Clear(); // clear the current output content from the
buffer
> > > > Response.AppendHeader("Content-Disposition", "attachment; filename="
+
> > > > file.Name);
> > > > Response.AppendHeader("Content-Length", file.Length.ToString());
> > > > Response.ContentType = "application/octet-stream";
> > > > Response.WriteFile(file.FullName);
> > > > Response.End();
> > > > > > Does that answer your question? If the problem is still there,
please
> > feel
> > > > free to post here.
> > > > > > Best regards,
> > > > Yanhong Huang
> > > > Microsoft Online Partner Support
> > > > > > Get Secure! C www.microsoft.com/security
> > > > This posting is provided "AS IS" with no warranties, and confers no
> > > rights.
> > > >
Hello James,
Thanks for your response.
The Response.End method ends the page execution and shifts the execution to
the Application_EndRequest event in the application's event pipeline. The
line of code that follows Response.End is not executed. So
ThreadAbortException exception occurs.
To work around this problem, For Response.End, call the
ApplicationInstance.CompleteRequest method instead of Response.End to
bypass the code execution to the Application_EndRequest event.
For detailed info on it, please refer to MSDN article:
"PRB: ThreadAbortException Occurs If You Use Response.End,
Response.Redirect, or Server.Transfer"
http://support.microsoft.com/?id=312629
Does that answer your questoin?
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hello Yan-Hong,
Thanks for you reply again!
I think this is a quite interesting case since when I use
ApplicationInstance.CompleteRequest instead of Response.End, no more
exception is captured. However, the original problem, HTML codes are
included in the downloaded file, comes again!
Until now, I think using Reponse.End with additional exceptional handler (in
which the remaining codes after Response.End are there) may be the best way
to solve my problem.
If you have any other good idea, please let me know and share with all
others here.
Regards,
James
"Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
news:Yxq4fBlrDHA.1212@.cpmsftngxa06.phx.gbl g...
> Hello James,
> Thanks for your response.
> The Response.End method ends the page execution and shifts the execution
to
> the Application_EndRequest event in the application's event pipeline. The
> line of code that follows Response.End is not executed. So
> ThreadAbortException exception occurs.
> To work around this problem, For Response.End, call the
> ApplicationInstance.CompleteRequest method instead of Response.End to
> bypass the code execution to the Application_EndRequest event.
> For detailed info on it, please refer to MSDN article:
> "PRB: ThreadAbortException Occurs If You Use Response.End,
> Response.Redirect, or Server.Transfer"
> http://support.microsoft.com/?id=312629
> Does that answer your questoin?
> Best regards,
> Yanhong Huang
> Microsoft Online Partner Support
> Get Secure! C www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no
rights.
Hello Sherif,
Thanks again for your reply!
Do you mean that I should put all remaining codes outside the
Try...Catch...End Try block? On the other hand, I don't understand what
class's Render method you suggest me to override.
Regards,
James
"Sherif ElMetainy" <elmeteny.NOSPAM@.wayout.net.NOSPAM> bl
news:OOAFiVcrDHA.2304@.TK2MSFTNGP11.phx.gbl g...
> Hello
> ThreadAbortException is special in that even if your handler catches it,
it
> will be rethrown again at the end of the catch block, unless you call
> Thread.ResetAbort(). In your case it is not appropriate to call
> ResetAbort(), because this means the rendering of the page will continue.
> If you don't want to call Response.End() which throws the
> ThreadAbortException, you can override the Render Method and not call the
> base class Render method.
> Best regards
> Sherif
Hi James,
Just now I found this good article for you. :)
It contains a good sample in ASP.NET on how to download and upload file.
"Downloading and Uploading Files"
http://msdn.microsoft.com/library/e...pmig-downloadin
ganduploading.asp?frame=true#aspnet-jspmig-downloadinganduploading_topic6
Hope that helps.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
Hi Yan-Hong,
Thanks again for your effort on my problem. I'll study the document to
solve my problem.
Regards,
James
"Yan-Hong Huang[MSFT]" <yhhuang@.online.microsoft.com> bl
news:CjFnTunrDHA.1716@.cpmsftngxa06.phx.gbl g...
> Hi James,
> Just now I found this good article for you. :)
> It contains a good sample in ASP.NET on how to download and upload file.
> "Downloading and Uploading Files"
http://msdn.microsoft.com/library/e...pmig-downloadin
> ganduploading.asp?frame=true#aspnet-jspmig-downloadinganduploading_topic6
> Hope that helps.
> Best regards,
> Yanhong Huang
> Microsoft Online Partner Support
> Get Secure! C www.microsoft.com/security
> This posting is provided "AS IS" with no warranties, and confers no
rights.
Hi James,
It is my pleasure. :)
Thanks very much for participating the community.
Best regards,
Yanhong Huang
Microsoft Online Partner Support
Get Secure! C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
0 comments:
Post a Comment