Send large String data from HTML/Javascript to code behind - c#

Our application has some hidden fields like any other asp.net application. Here we are using hidden fields to store HTML of an image, which is considerably large. We use the value(large HTML string) of hidden field in our C# code for further processing.
We tend to create 4 instances of our application and we have doubts over load balancing of azure cloud service. We assign values to these hidden fields in midway of our application process through javascript. As this processing is done on client side there are no issues here. But since azure has multiple instances so if want to access these hidden fields on server side (i.e. in our C#), accessing these hidden fields directly would create any problem due to load balancing if the instance changes ?
Note: Our page does not postback while accessing these hidden fields
on the server side.
We are not clear when does the instance change, if our page does not postback then will the request go the same instance ? Is this guaranteed.
Also if the page does postback then does the response goes to the same instance of the calling request instance.
We need suggestions or the correct way of accessing these hidden fields on the server side. These hidden fields are very important to us; using the cache/session settings of azure will become very costly for us since the data is very large. It would be very helpful if the suggestion would be for cost free implementation. As we are already running on a tight budget.
//25Oct 2013
We have a large string of data, which is majorly made up of HTML obtained from Bing Map, we have taken the HTML of the Bing map using the Jquery Selector of our Bing Map div element, we want this HTML string to be sent to the code behind. We have this string on our javascript but when we do ajax call to the code behind it fails.
We even tried to send it in body via a POST method, but this fails again.
var string = formData; // so long text
var xhr = new XMLHttpRequest();
var body = "string=" + encodeURIComponent(string);
xhr.open("POST", "index.aspx/getString", true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.setRequestHeader("Content-Length", body.length);
xhr.setRequestHeader("Connection", "close");
xhr.send(body);
As Rick suggested, we had already tried blob but the problem is with sending the string from javascript to code behind. We are really stuck on the dead end here.

You indicated that caching would be costly for you. While it is true that using the Cache Service would incur some costs, have you considered co-located in-role caching as an alternative? Perhaps you have enough extra resources on your existing instances to support your needs. There is a link to some capacity planning in the link I've provided above if you choose to take this route.
--- 10/24/2013 ---
If I'm understanding your latest description correctly, you are generating some HTML on the client that you want to upload to your web app on Azure. In the web app, you're using that HTML to generate a PDF that I assume the client would later receive. If this is your scenario, then you could just upload the HTML and store it as a blob? This way any instance of the web app can reference it from blob storage rather than stuffing it into hidden fields.
In your web app, you can use HttpPostedFile to receive the file from the client and save it to a blob. Note: You may need to adjust the max. size allowed for the post since it defaults to 4MB and you indicated your data could be up to 5MB.
I'm trying to help you but your question is just not that clear. Even the title is misleading given the context of the discussion. If this doesn't help, then you may want to edit your question.

Related

Need some inspiration on how to generate and stream images efficiently on Azure with C#

I have a situation where I need to display an image to an end user following the below method.
When the user request the image from a URL, the C# code behind should start by looking in an azure blob/cdn to see if the image is there. If the image is there and less than x days old, it should pass the image to the end user in the most efficient way (preferably without spending too many resources (memory & cpu) passing it to the user.
If the image is not there or more than a week old, the image will be generated based on the parameters supplied in the url the user requested, after which it is stored on the blob/cdn and displayed to the end user.
My problem is how, I in the most efficient way on azure, can generate a lot of images simultaneously as well as being able to pass the data from the cdn while still being able to first check if it is too "old" and needs to be regenerated or if it isn't there and needs to be generated before being displayed to the user. Since the second I pass the image through the c# code will loose the cdn's strengths.
There are many ways to do things on azure. You will need to look through the extensive azure documentation and determine what will best fit your needs.
If you want to get something working quickly azure blobs can be served directly to a client (straight to their browser) - see the doco keep in mind anonymous access is not secure.

Is it possible to derive(fake) a location path to an in-memory MemoryStream file?

Is it possible to derive a location path of an in-memory file?
My justification for doing this is based on having a collection of images that are retrieved by my WinForms application in a Base64 encoded string format. I need to build up some HTML and inject these images so that they can be rendered on an embedded page in a WebBrowser control on my application. Since we're talking about HTML here, I need to use the <IMG> tag to display the image. This element needs to accept a "src" path which means I need to determine a method of deriving an absolute/relative path to each of the in-memory images.
If you control the application running on the server (which you indicated in a comment), then you should be able to redirect requests for particular resources. For example, if the user application requests "http://myserver/memory/imgxxx.jpg", the server should be able to intercept that and, rather than try to serve imgxxx.jpg from disk, construct an image from the data in memory, and ship it down to the client.
Now, if you're just shipping the base64 encoded data to the client, and want the client to somehow access the data and do the conversion ... that's a harder problem. I can envision doing something with JavaScript to replace all of the img tags that have some given attributes with the corresponding image. But I suspect that'd get pretty messy. If it's even possible.
Another possibility is to create a derived WebBrowser component and customize its behavior. A good example is in the CreateSink method documentation. It might be possible to write a handler that is called whenever the component wants to download something. You could then intercept the call and supply your in-memory image. I'm not certain that this is possible. You might take a look at WebBrowser customization. I will say, though, that it's probably easier to just write the files and use a "file://" url.
You can build a light weight HTTP server into your application by using the HttpListener class.
You will want to use the asynchronous model. Create a url that it serves content to, such as "http://*:8080/appdata", and then use that url within your html (http://localhost:8080/appdata/someinmemoryresource").
When theHttpListener receives a request, look at the path and respond with whatever in memory data it is you want to serve!
The IMG tag does not need a path, it needs a URL. You cannot reference in-memory data with a URL. The browser is going to issue a second HTTP request with the URL provided in the IMG tag and that URL must be valid at this point.
Consider adding an MVC action or an HTTP handler to return the image bytes. Or save it to disk.

Upload files without full page postback

The Plan
I need to upload a file without causing a full page refresh.
The uploaded files should be stored in a temporary place (session or
cookies).
I will only save the files in the server, if the user
sucessfully fills all the form fields.
Note: This is one of the slides of a jQuery slider. So a full refresh would ruin the user experience.
The Problem
If I place a Fileuploader Control inside a AJAX Update Panel, I wont be able to acess the file on the server side.
Note:From what I have found so far, this happens due to safety reasons.
Can't be done without co-operating binaries being installed on the
client. There is no safe mechanism for an AJAX framework to read the
contents of a file and therefore be able to send it to the server. The
browser supports that only as a multipart form post from a file input
box.
The Questions
When storing the files in a temporary location, should I use session or cookies? (what if the user has cookies disabled?)
If preventing a postback, really is against the standarts of user safety. Will it harm my website reputation? (regarding SEO and such)
Which road to take?
C# ASP.Net with AJAX? (is there a workarround?)
C# ASP.Net + AJAX Control Toolkit? Does it helps? (using the AsyncFileUpload control)
C# ASP.Net + jQuery Control? (won't I have problems fetching the data from the JavaScript?)
C# ASP.Net + iFrame? (not the most elegant solution)
The total amount of cookies that you can use is limited to a few kilobytes, so that's not a viable option to store a file. So sessions would be the only remaining. Consider also to save the file in the file system and remove it if it's not going to be used, as storing files in memory (session) will limit how many users you can handle at once.
No, for functions like uploading files you don't have to worry about that. Search engines doesn't try to use such functions when scanning the page.
You can use an AJAX upload in browsers that support direct file access, but there is no way around doing a post if you need to support all browsers. However, the post doesn't have to end up loading a new page, you can put a form in an iframe, or point the target of a form to an iframe.

How to pass XML data to web page running in web browser control?

I have windows application in which web page is loaded using the windows (.NET) browser control. The windows application needs to send some information to the web page (aspx) and now it's achieved using query string.
Now we want to send more details which may be in the XML format.
How can we pass the large amount of data (like XML) in such case?
If you want to send (more) data, you should use method POST. (That way you don't really need to care whether it's XML or something as long as the particular form can handle it correctly.)
Query string should be used only to specify the resource. Typically query string is used with GET method like http://server/giveme.php?report&number=123. (If that string is used with browser, it should open connection to server:80 and say GET /giveme.php?report&number=123. (Then some headers follow, etc.)
Use of POST is very similar except that after the method, URI (/hereis.php?report&number=124) and some HTTP headers, also data is sent (which can really be any data).
Also remember that GET is supposed to be used only for queries: you can call GET /giveme.php?report&number=123 a thousand times and you should still get the same report no. 123 (or some updated version). (So GET should not be used to send data, but to get data.)
For POST, this is not expected: Each time POST from you is accepted, you actually post some data to the server, and normally you should not get away with doing POST /hereis.php?report&number=124 multiple times. (Well, you can design your application to accept POST /hereis.php?report, but that's up to you and it's another story.)

how to download a partial web page using .net

We are downloading a full web page using System.Net.WebClient class. But we only want less than half of the page. So is there a way to download a portion of the page, say 1/3rd, half etc of a page using .net library so that We can save the network bandwidth and the space? If so, please throw your ideas, thanks.
You need to provide an "Accept-Ranges header" to your GET or POST request. That can be done by using the AddRange method of your HttpWebRequest:
HttpWebRequest myHttpWebRequest =
(HttpWebRequest)WebRequest.Create("http://www.foo.com");
myHttpWebRequest.AddRange(0,100);
That would yield the first 100 bytes. The server, however, needs to support this.
The sort answer is not unless the web app supports some way to tailor it's response to what you want it to return.
This could take the form of a
query string parameter
header field value
The simplest way to add this would be a query string parameter and when detected write out the necessary HTML to the response object. If you are unable to make changes to the web app then you won't be able to control how much of a page is returned to you.
You might want to read up on how HTTP works since the question and it's answer relies upon this. Specifically the Header Definition should be helpful.

Categories