MVC3 - File Download - Wait Status indicator - c#

Ok, I've done my homework and have found similar threads. However, I haven't found a good answer.
Using MVC3, C#, Razor View Engine.
My scenario is pretty straightforward. I have a view with a link that calls an action on a controller. That action returns a file that was dynamically generated. The process takes anywhere from 1 to 10 seconds. During this time I want to lock the UI and display a "Please Waite" message.
My first attempt used something like this:
#Ajax.ActionLink("my test link", "myAction", new { Controller = "myController" }, new AjaxOptions { OnBegin = "ajaxStart", OnComplete = "ajaxStop" })
The ajaxStart and ajaxStop functions then used the jquery blockUI script to block and unblock the UI and display the "Please Wait" message. This worked in that it showed the message, but no file would download. After some more research, I found that I could not use Ajax to initiate a file download. If I'm wrong, please enlighten me.
So, I'm back to a normal ActionLink. This works in that I can download a file. I can even catch the .click event and block the UI and show the wait message. However, how do I know when to unblock the UI? How can I know when the file save/open dialog has opened up? Perhaps if I could catch that event I could then unblock the UI.
I have seen the other posts that recommend a much more complex solution by breaking the file generation/download into separate functionality. I very much want to avoid having to save the file on the server, or having to poll the server to see if the file is done yet. This should be fairly simple.
If anyone has any ideas, please let me know.
Thanks,
Tony

there was a post awhile back on this on SO:
Detect when browser receives file download
It referenced this link: http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
Effectively what it does is it sends a cookie (C# Generated) with the file (that the user is downloading) to the client. Once the client has the cookie, theoretically this happens after the file is downloaded). Javascript will check to see if the client has that cookie, if so Javascript will unblock the UI.

Related

Simulating Javascript 'doPostBack()' in C#

I am writing a web scaper for my company. Our client gives us access to their website for this purpose, but our client's IT team does not communicate with us, so I have to do the program with no help from the source.
Their website uses javascript on all of their buttons/dropdown menus to send postData to the server so that the screen will update to show the end user the correct info.
I am trying to get my program to simulate clicking the 'next page'. The 'next page' button has an onclick event that reads like this...
onclick="javascript:WebForm_DoPostBackWithOptions(
new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$ucTaxQueueListView$lviewOrderQueue$DataPager2$ctl00$btnNextPage"
, "", true, "", "", false, false))"
In my C# program, i am using the HTTPWebRequest class and the HTMLAgilityPack to do my requests / scrapping respectively.
I've done all i can in my code to try and get this to work. The only thing that works is to use Fiddler to copy the postData and paste that verbatim into my WebRequest function. This is very impractical when i have to potentially go to 1000+ 'next pages'.
I have also tried extracting the ViewState from page and using that, but that always gives me an 'error' page.
Any help or guidance would be appreciated and even compensated...my boss wants this project completed this weekend!!!
The last time I had to do a project similar to this, I took a very different approach.
I used GreaseMonkey -- though you could also use a Windows HTA file with the same effect --
And I let the GreaseMonkey script run and step through the pages one by one. To handle the DoPostBack I simply invoked the click handler on the appropriate elements.
I had several data stores going.
One DataStore covered every menu item that I had "clicked" on to avoid duplicating things.
Another DataStore was the raw HTML of the page (taken by body.innerHTML)
Once I had cloned all the pages, I wrote another GreaseMonkey script to load up each saved page and mine whatever info I needed off of it. I build up a third datastore of resources (images and CSS) and then pulled those down with a big text file piped into CuRL.

Possible to show an on-page progress while downloading a file?

On a website, I'm successfully using an ASHX handler to download files from the server to visitors of my website.
A customer suggested to not rely on the browser UI to visualize the download progress but instead provide some progress on the page to provide a more visual cue.
I.e. first, the user sees a download page with a link/button:
Then, when clicking the Download your file now! button something like this should appear:
Unfortunately, even after searching Google and SO for hours, I found zero similar topics. All occurrences deal with upload, not download. Either I'm using the wrong keywords or there are really no solutions.
Therefore my question:
(How) would it be possible to provide such a download progress?

Show Message After a File has Downloaded in a New Window

The user fills in a form to download a file. The form results load in a new window (target="blank"). The MVC Controller Action returns a FileResult on success or my "SelfClosingPage" view on failure.
The goal behind this is to have the user download the file in a new page, and if any errors occur, the original calling page's url doesn't change (to the /DownloadFile url) and the user remains on the form page, instead of being directed to an error page.
This all works great, except I need to know when the file download is complete because I'd like to 1) hide the "File is downloading, please be patient" message if the download is successful 2) show an error message if the file download failed.
I was using a Cookie to do this and a JS interval to regularly check the cookies value. It either never worked or doesn't work any more (I can never get the cookie to show up on the original page).
Please advice. I can't use C# code in my JS because well, it wouldn't work since it executes once when the page is loaded and I'm trying to decouple the JS from the C# code.
I think my only solution is to do ajax javascript callbacks, but I'd like to avoid that.
UPDATE:
Found these related SO links that use the same approach I was trying to use.
MVC3 - File Download - Wait Status indicator
Detect when browser receives file download
Update 2
It's working again. I think the cookies expiry date was not long enough (though it should have been). I just changed it from 10 min (a file download should not take longer than that) to half a day.

How to create a File Upload with progress data?

I'm developing a web (using asp.net and c#) which has a FileUpload control from asp.net. The upload thing works perfect and as far as I know I can't show progress data (%, bytes transfered, upload speed, time elapsed, time left, progress bar) using the FileUpload control from asp.net because its not asyncrhonous.
I've searched a lot (really) on the internet and I didn't find what i'm looking for and too much info has become a big confusion since I'm not sure about what I have to use.
On my web page I have a file named "UploadFile.aspx" which has a FileUpload control and a button that handles the uploading. On code-behind (UploadFile.aspx.cs) I have all the server-side logic (Upload the file into specific folder, store info about that file into a database, etc. etc) and I don't want to change this.
What I need to know is how to show the progress data to the user while is uploading the file? I can't use 3rd party applications because this is for an important commercial site. It's not a problem for me if I have to learn javascript / jQuery / Whatever but really i'm a bit lost and I don't know how to start.
Thanks for your time and your help guys.
There's some pretty cool solutions out there. Granted, you can code your own, but I'd suggest using a jQuery plugin like Plupload. If you need help setting it up, you can read their documentation.
There are lots of lots of demo code are available on the net to show the progress bar with file upload control in c#, most of them work fine on Local system but never work on the live server, Because You CAN'T USE A FileUpload control for what you want to do. When a user POSTs a file, you have to think of it like a querystring parameter. It goes as one Http Request. If you want to do a progress bar you'll want to look into something that can interact with the server asynchronously.
If you don't want to use any 3rd party that relies on Flash / Html 5, please take a look at this article:
http://vanacosmin.ro/Articles/Read/AjaxFileUpload
This is possible (and if you're using .NET 4.5 GetBufferedInputStream will make your life easier), but it is not very easy, as you'll see.
Basically, if you want a file upload with progress bar that is fully compatible with every browser, you need to handle this server side and give an url where the client (the browser) can check periodically for the progress with ajax.

What is the life cycle of an ASP.NET file upload on code behind?

I would like to know what events get executed (if any) after a user has selected to submit a file for upload on an ASP.NET page.
Would I need to tweak anything at the IIS level?
Should the page life cycle events be triggered when the user uploads a file?
I see different behaviour on my development server from Visual Studio in regards to the IIS server I deploy to:
on the development server, the life
cycle events get triggered when a file
gets selected, on the deployed server
they don't..
What classes would I need to override, what web.config settings should I change, in order to tweak the default behaviour of the upload?
The problem I am having, is finding documentation on how to have code executed before the file gets uploaded, but after a file got selected.
P.S. this is related to a previous question of mine here, but approached in a very different way in the hopes of understanding the whole upload process, so I thought it's a different question all together.
No, it has no event for file upload(maybe on .net 4)
what I did was a class that derives from the upload object and on OnLoad event something like this:
public event EventHandler OnUpload;
protected void OnLoad(...){
if (this.HasFile && this.OnUpload != null)
this.OnUpload(this, EventArgs.Empty);
}
sort of that.
Joe
-- Edit:
Oh, I re-read your post and you want to know when people selects a file but it is not yet sent to the server?
thats javascript.
Server side can't know when people select it without sending a information to the server.
You can develop a WebService function that is called when the onchanged of the input has been called, but I don't know if thats a good idea.
Can you use javascript for this?
try:
<asp:textbox id="t1" runat="server"/>
on code behind:
t1.Attributes.Add("onchange", "alert('it changed its value: ' + this.value);");
hope it helps.
To execute code before upload, it would have to be JavaSript code running in the browser. See my answer to a question about filtering file uploads based on type:
How to filter which files can be seen on upload dialog box?

Categories