Sending ICalendar event to client while still continue running your code - c#

I have built a basic calendar event using DDay.iCal, when I click "Add to calendar" link I produce an event and then sends this to the client.
Basically, my application works like this.
A User logs in.
Selects a specific date.
Books a specific timeslot
Clicks the "Add to calendar" link
Sending the event is done by using Response.Write() which sends the following to the client:
Response.ContentType = "text/calendar";
Response.AddHeader("Content-disposition", "attachment; filename=appointment.ics");
Response.Write(iCalString);
The above works fins but it requires me to first book the event then manually and then click the "Add to calendar" link.
I want to merge the steps 3 and 4. But when trying to do so the event booking gets saved to the database but the screen does not get refreshed.
Is there a "simple" way to get around this?

You need to set up a simple HttpHandler to post the reply and then do a Response.Redirect() into it.
I have done similar work before with integrating the vcard format into an our people section of a website. These articles should tell you everything you need to set up the HttpHandler - just replace the vcard code with your ical code.
http://professionalaspnet.com/archive/2007/09/23/Streaming-a-vCard-on-the-Fly-in-ASP.NET-with-a-Custom-httpHandler.aspx
http://weblogs.asp.net/gunnarpeipman/archive/2009/08/09/creating-vcard-with-image-in-net.aspx
When you redirect into the ical handler it will just instantly pop up the download box, you wont be taken to an empty page, the user is left on the same page.

Make sure that you clear your response (Response.Clear) prior to sending the iCal response information. Finally, end your response (Response.End) before any other content can be emitted. From the end use standpoint this will produce the same result of using an HttpHandler without the hassle.

Related

Use indicator for downloading file in mvc

We have a Html.BeginForm(...) with a download button in it. When the button is clicked we enable a busy indicator prior to the submit. The form posts to the controller and the controller returns a FileStreamResult with the response headers like set so the file is downloaded instead of opened or begin redirected.
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=downloaded-file.pdf");
// Return from the controller.
return new FileStreamResult(GetTestFile(), "application/pdf");
I need a way to now disable the busy indicator once the controller returns since there is no redirection.
One solution that i recommend is to detect when browser receives file download
It referenced this link and this link
The ideea is following: it's sends a cookie (C# Generated) with the file (that you are downloading). Use a window.setInterval to query for the presence of a cookie at regular intervals and check its value,and, if you have that cookie you can disable busy indicator.

Unable to download file (page url same as download url)

I'm trying to download a zip file (that is normally accessed/downloaded by pressing a button on a web page) using C#.
Normally the file is downloaded by selecting "Data Export" and then clicking the "SEARCH" button at this URL:
http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=en-GB&fromdate=2016-05-30&tomdate=2016-06-03
If trigger the download manually on the webpage and then copy the download url from the 'Downloads' view of chrome or firefox I get the exact same URL as above. When I paste that in a browser window I will not trigger the download, instead the above page will be loaded and I have to trigger the download manually in the same way as in the first place.
I've also tried using the network tab of the inspector to copy the request header of the request that is triggered when clicking the "SEARCH" button, but that URL is also the same as the one above.
Trying with C# I get the same result, the page itself is downloaded. My code looks as follows:
using (var client = new WebClient())
{
client.DownloadFile("http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=sv-SE&fromdate=2016-05-30&tomdate=2016-06-03", "zipfile.zip");
}
My guess is that my code is correct, but how do I get the correct URL to be able to download the file directly?
ASP.net inserts a bunch of crap into the the page to make things like this particularly hard. (Validation tokens, form tokens, etc).
Your best bet is to use a python library called Mechanize, or if you want to stick to C# you can use Selenium or C# WebBrowser. This will fully automate visiting the page (you can render the C# WebBrowser invisible), then just click the button to trigger the download programatically.

Sending Http requests from a webbrowser to HttpListener

I've set up an HttpListener running on port 8086 on my custom server application, which works with JSON and XML over sockets. I can't change my server application to any other implementation, I am stuck with it for now.
I'm a bit new to Http, so feel free to correct my naviety below! I've searched, but can't find anything that deals with my exact problem.
When I use Hurl.it to send an HttpRequest to the server, the GetContext function fires no problem and I am able to send an html response back to Hurl.it.
Now, the question is, I want to be able to send out Reset Password email links in the form of "http://myip:PORT/resetpasswordseed=74283235" and have a browser be able to receive, parse and display a dynamically generated html form. (I will also need to add SSL to this a bit later, as I don't want a newly updated password being sent as plain text in a URL.)
Now, when I click the URL link http://myserverip:port/resetpassword with Edge or Firefox the GetContext function does not fire and the web browser times out. Have I missed something or am I trying to do something that is not possible? I don't get an error message or an exception or anything.
Any help would be appreciated.
Cheers
Craig.

Side actions not happening after Response.WriteFile()

As per an ASP.NET page, on click of a button the following actions should happen:
Download a file from server
Perform clean up actions like
Hide a Button
Set text to a label
Display the label
Disable a button
etc.
Now the download part happens through the following code:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename="+strFileName+".pdf");
Response.WriteFile(strBrowserPath);
And the remaining actions (Point 2) are done after.
Sadly, since Response.Clear()/Response.WriteFile() is used the remaining server side actions are not happening.
Any alternatives? Any fixes for this?
From the moment you use the post back action to send a file, at the same time you can not send and the page with the changes (disable button, change text, etc). There is no fixes for this because there is not a problem, is the way its works. You have one pipeline to send your response back. You ask for one http response and you get one http response, you can not have both update page and file.
Now, alternative. First of all is how you going to send the file. I suggest to use a handler with parameters like download.ashx?Dhwoload=filea.pdf and after the post back with the update on your page, you call the
window.location = "download.ashx?Dhwoload=filea.pdf";
javascript, with an alternative link as
if the file did not start to download automatically click here
and make your work.
relative:
What is the best way to download file from server
Error handling when downloading file from ASP.NET Web Handler (.ashx)

After disabling a Submit button with JS, re-enable after file download

I'm working on an ASP.NET application. Currently when the user clicks "Save" on an item, we disable the Save button, change its text to "Saving..." and so on. This works fine, because the button causes a postback which results in a new page being loaded, on which the Save button is no longer disabled.
We now want to apply this to other actions: Publish (not much different from Save), Import (again, much like Save, but based on data the user has uploaded in a previous step), Export (downloads an XML file to the user) etc.
Export is causing me problems - the button stays disabled. I think this is because the server sends back an XML file rather than a new web page, and so the browser just displays the same page.
The server-side code is along the lines of
Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + whatever);
[push file contents into Response.OutputStream]
Response.End();
[No idea if this is good code or not - it's not mine - but it does the job :)]
Basically, I'd like to know either:
a way of making the server send a fresh page back in the response as well as the XML, thus re-enablnig the button in the same manner that the other pages do, or
a way of getting the browser/JS to re-enable the button once the file has been sent.
Looks like this should do it: essentially, set a cookie with the file response, and have the browser waiting for that cookie in order to unblock the page.
The problem probably is that you do not load a new page at all.
Since content disposittion is attachement, the browser will not reload the page but only save the return from the server to disk.
You need to reload the page some how but I have no really good idéa on how to do that after a file fetch.
There is a dirty hack. You can use setTimeOut method to enable and change back button caption/image.
So you can write a server side code similar to
btn.Attributes.Add("onclick", "this.disabled='true'; this.value='Processing...';_doPostback(this,null);setTimeout(function() { enable button logic....set the text /img of the button}, );");
The server side download attachment event will not be synch perfectly, but you can set timeout as 2-5 seconds depending on your server configuration.
Thank you!

Categories