Insert Anywhere A String Into The Response Stream?
May 7, 2010
I created a simple Http Module that starts a timer on the PreRequestHandler and stops the timer on the PostRequestHandler to calculate the time it took the page to load. I then create some simple html and write my results to Response.Write. Since I'm doing this in the PostRequestHandler it's adding my results after the </html> tag. That's fine for testing but I need in a scenario where the page needs to validate.
I can't seem to figure out how I could manipulate the Response object to insert my results before the </body> tag. Response.Write and Response.Output.Write don't have that flexibility and I couldn't see a way to work with the Response as a string. Am I missing something easy?
I have an excel file in my Response Output stream. I can Open the stream as a file after a prompt, but it doesn't seem I can save it directly to a specified folder on my client.
I'm trying to return an xml string from a IHttpHandler to a like this: context.Response.Write(xml); When I receive the response in my .aspx.cs I try to load the document as follows: var xml = XDocument.Load(xmlString); but I get an Illegal Characters in Path error message. I've also tried
in previously asked question answeres said they dont get what i want to do exactly so heres is the full code also i simply want that instead of a TABLESe i rendered an image( of the content ) on the page
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.IO; using System.Text; using System.Data; using System.Drawing; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { System.Web.UI.WebControls.Panel panelmain = new System.Web.UI.WebControls.Panel(); System.Web.UI.WebControls.Literal abc = new System.Web.UI.WebControls.Literal(); abc.Text = "as<br/>dasdas<br/>dasdad"; DataSet ds = new DataSet(); DataTable dt; DataRow dr; DataColumn idCoulumn; DataColumn nameCoulumn; dt = new DataTable(); idCoulumn = new DataColumn("ID", Type.GetType("System.Int32")); nameCoulumn = new DataColumn("Name", Type.GetType("System.String")); dt.Columns.Add(idCoulumn); dt.Columns.Add(nameCoulumn); dr = dt.NewRow(); dr["ID"] = 1; dr["Name"] = "Name1"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["ID"] = 2;.............
MemoryStream export = new MemoryStream(); iCalendarSerializer serializer = new iCalendarSerializer(iCal); serializer.Serialize(export,System.Text.Encoding.UTF8); return export;
so I am using the C# DDay.iCal library for exporting my calendars. Serialize takes in a "stream" so I passed it a memory stream. I now have a generic handler that calls the method that contains the above code.
public class CalendarHandler : IHttpHandler { private Calendar service; private Membership membershipS; public void ProcessRequest(HttpContext context) { service = new Calendar (); membershipS = new Membership (null); string userName = context.User.Identity.Name; Guid userId = membershipS.GetUsersId(userName); context.Response.ContentType = "text/calendar"; // calls the export calendar(the code that showed above that uses dDay ical. var t = service.ExportCalendar(userId); t.WriteTo(context.Response.OutputStream); } public bool IsReusable { get { return false; } } }
So now I wrote the icalendar to the Outputstream. Now I have a jquery post that goes to this method and now I am not sure how to take the OutputStream result that the jquery post will get and make it popup with a save dialog box.
We're getting this weird exception when trying to create a BitmapDecoder off an ASP.Net response stream. This is the line of code that throws the exception:
BitmapDecoder dec = BitmapDecoder.Create( Request.Files[0].InputStream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.OnLoad);
Here's some info on the file being uploaded: Filename: bank statement.jpg, Content length: 266041, Mime type: image/jpeg
This is the exception stack trace:
System.IO.IOException: Cannot read from the stream. ---> System.Runtime.InteropServices.COMException (0x88982F72): Exception from HRESULT: 0x88982F72 [code]...
Is is possible to update an image on an HTML page using the response stream of an ASP.NET Generic Handler? For example, if I have the following code on a handler: [URL]
How can I call the handler in jQuery, using .ajax, and set an image on the page equal to the image that is returned by the Handler? I am ignoring the query string variable at the moment as I just want to get this working. Secondly, if I can do above, how can I loop through all of DIVs on the page that are of class "content", select a guid out of a hidden field to use as my query string parameter, and update the associated image within the same content DIV tag? A content div tag would look as follows:
I would like to have all the images updated on a periodic interval, so essentially, as a quick recap, I would need to have: A loop through all of my DIVs of class "content", occuring every n. seconds. Extractation the GUID from the hidden field. A call to my ASHX handler to get the updated image. Set the returned image to the corrisponding image on the page. Is this something that would be difficult to achieve? If not, what would I need to do to make this function in the above manner?
Is is possible to write to the http response stream from a dynamically created bitmap using the Response.Write/WriteFile without saving the image to the hard drive?
I'm using .net 3.5 and am currently creating a web application used to generate a report through Aspose.CellsActually, the page is composed in a form where I get the configuration of the report I have to generate. The "generation" button is in an update panel. When I click on it, the "generation" button is hidden and a progress bar appears. When the excel file is generated, I save it in a memory stream and I send it back to the aspx page where I change the headers to allow the file's download.
After a call to initial HttpWebResponse.GetResponseStream() and reading through the stream, that stream is done for and cannot be reused.
I have a situation where I need to examine the content of the response and if it is of a certain data, get another page and then pass the new response down the line. Otherwise, pass down the original response as is. The only problem is that after examining the response to check for this "special data", that response is no good to the downstream code.
The only way, I can think of, to make this transparent to the downstream code, is to create a derived class of HttpWebResponse, and somehow cache the data streamed, and pass that cached stream down the line instead of the initial stream. I'm not sure if that's even feasible since I haven't looked into it further.
I am trying to use a custom ITempDataProvider provider to store TempData in a browser's cookie instead of session state. However, everything works fine except that I am unable to remove the cookie from the Response stream after reading it. The code exexutes fine but the cookie won't go away.
I'm using Filestream for read big file (> 500 MB) and I get the OutOfMemoryException. Any solutions about it?? I want this in my app asp.net: Read DATA from Oracle Uncompress file using FileStream and BZip2 Read file uncompressed and send it to asp.net page for download. When I read file from disk, Fails !!! and get OutOfMemory. My Code is:
using (var fs3 = new FileStream(filePath2, FileMode.Open, FileAccess.Read)) { byte[] b2 = ReadFully(fs3, 1024); } // [URL] public static byte[] ReadFully(Stream stream, int initialLength) { // If we've been passed an unhelpful initial length, just // use 32K. if (initialLength < 1) { initialLength = 32768; } byte[] buffer = new byte[initialLength]; int read = 0; int chunk; while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0) { read += chunk; // If we've reached the end of our buffer, check to see if there's // any more information if (read == buffer.Length) { int nextByte = stream.ReadByte(); // End of stream? If so, we're done if (nextByte == -1)............................
I have an httpmodule and it has a handler for OnEndRequest. I am trying to write the HttpContext.Response.OutputStream to a file. I am trying to use the Read method of it. But when it is called the exception message i get is "Specified method is not supported". So i am not able to do what i want that is, write the response to a file on the disk. I am able to write the HttpContext.Request.InputStream to a file using its Read method which i do in a handler for OnBeginRequest.
I am generating an Excel file upon a click of a button in an update panel. It is throwing a parsing error.
If I keep the button outside the update panel it is working fine. Why isn't it working in the update Panel?
Response.ContentType = "application/vnd.ms-excel" Response.AddHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)) Response.Clear() Response.BinaryWrite(WriteToStream.GetBuffer) HttpContext.Current.ApplicationInstance.CompleteRequest() Private Function WriteToStream() As MemoryStream 'Write the stream data of workbook to the root directory Dim file As MemoryStream = New MemoryStream hssfworkbook.Write(file) Return file End Function
I need to to export a file to the user. It takes 1-2 min to generate the file so I'd like to have the page go into a kind-of modal mode with a layover on the page and a 'Working' spinner showing. The problem is I can't make the modal stuff go away after Save File dialog is closed.
How can I remove the layover after the dialog is done?
I am getting my Request from a third party application(different domain) to my ASP application. I am handling the request and doing the business part in my application and as a acknowledgement I need to send XML string as Response to the same Page which POSTED the request to my Application. I was successful in retrieving the input from Request using the following code
NameValueCollection postPageCollection = Request.Form; foreach (string name in postPageCollection.AllKeys) { ... = postPageCollection[name]); }
But i am not sure how to send back the response along with XML String to the site(different domain)? EDIT: How to get the URL from where the POST happened.
How can I remove the html content and add new string to the Response object. If I use the Respose.Write method the page contains the string, but while taking the view source option from browser it will display some html tag like Doctype, head, body etc. My requirement is only the string should be displayed in the source.
I have some code that is used to replace certain page output with other text. The way I accomplish this is by setting the Response.Filter to a Stream, Flushing the Response, and then reading that Stream back into a string. From there I can manipulate the string and output the resulting code. You can see the basic code for this over at [URL] However, I noticed that Page Caching no longer works after the first Response.Flush call.
I put together a simple ASP.NET WebApp as an example. I have a Default.aspx with an @OutputCache set for 30 seconds. All this does is output DateTime.Now.ToLongTimeString(). I override Render. If I do a Response.Flush (even after the base.Render) the page does not get cached. This is regardless of any programmatic cacheability that I set. So it seems that Response.Flush completely undermines any page caching in use. Why is this?
extra credit: is there a way to accomplish what I want (render output to a string) that will not result in Page Cache getting bypassed?
I have a page that is used to add info from a gridview into a user's profile. I have explained my issues after all of the code. Here is the applicable code(s) involved:
Protected Sub AddToCart(ByVal sender As Object, ByVal e As CommandEventArgs) For Each gvr In GridViewItems.Rows Dim txtGiftAmt As TextBox = (CType(gvr.FindControl("txtGiftAmt"), TextBox)) If txtGiftAmt.Visible = True Then Dim customPrice As String = txtGiftAmt.Text.ToString 'HERE is where I would like to set the new price variable value somehow End If Next Dim scart As New ShoppingCart scart.AddCartItem(e.CommandArgument.ToString()) Response.Redirect("~/ShoppingCart.aspx") End Sub
This sub is called by the following button when clicked:
[Code]....
Here is the sub from the ShoppongCart Class named AddCartItem:
[Code]....
Here is the problem that I am having: I have one specific item that allows user's to define their own price (for this item only). By putting this price in a session variable seems to be the easiest way to handle this (at least for me), but the session variable's value always "lags behind". By this I mean that the first time the items is entered, it uses the default value of cartItem.ItemPrice. The second time the same item is added (with the intention of a different price), this new item's cartItem.ItemPrice is set as the first session("customPrice").
Because the user is selecting his/her own price for this item, I am not worried about lack of security involved with using query string to pass this price, but I can't get the string passed to the ShoppingCart class correctly (shows up null). Is there a way to append this customPrice to the current page's query string before the Response.Redirect takes place? If I understand this correctly, the Response portion "posts back" to the current page before the Redirect sends us on our merry way, right?
I have a create account page and on the page I have one button to insert all the details into two seperate tables one of the tables Pictures is dependant on the User table 1:1 relationship via UserID.
I have written some code to try get the last insert id so I can insert into the pictures table:
How to programatically insert string with special characters into RTF? I have rtf template I load to string and then replace all $MY_VARIABLE$ with data. Data contains special chars like 'ąęść' and the problem is that in result file these characters are replaced with '?'. It's something wrong with encoding but what?
My code looks like:
StreamReader reader = new StreamReader("template.rtf"); StringBuilder form = new StringBuilder(reader.ReadToEnd()); // here I replace variables in rtf with data Encoding srcEncoding = new UTF8Encoding(); Encoding dstEncoding = new ASCIIEncoding(); byte[] utf = srcEncoding.GetBytes(form.ToString()); byte[] asci = Encoding.Convert(Encoding.UTF8, Encoding.ASCII, utf); return dstEncoding.GetString(asci);