Best way to handle a 404 in a file download HTTP handler? - asp.net

I've got a grid which provides some links for users to download files. Files are stored outside of the application, the path references are read from the database and a HTTP handler (*.ashx) is used to serve each requested file. It is, however, possible that there could be a database entry pointing to a non existent file. I catch the FileNotFoundException, but I'm not sure what would be the best method to inform the user of the missing file (so that they can contact support).
First idea is to set a standard 404 code on the response, and that's what I'm doing now.
A more helpful way would be to display a notification (jQuery) about a missing file, but the file download is not done in AJAX, so this would involve a two step process - a client side onclick handler calls a web service method to check if the file exists, if not, then I cancel the click (return false) and display a friendly message to the user. If the file exists however, I proceed with the normal execution. But this adds yet another server call.
Have you dealt with a similar problem? How did you solve it?
Some clarifications - the application is built in ASP.NET 2.0 and uses jQuery to call the web service methods.

Pawel, it seems you've answered your question already...
...(so that they can contact
support)...
By stating the above I would suggest you create a custom 404 page which notifies the user of the file not existing on disk and provide them information on how to get in contact with the support office.
I've created a HTTP Handler for handling files and if a file does not exist on disk then I return a 404 response. I've setup IIS to display a custom page if a 404 reponse has been thrown.. (and I do the same for error 500).
Hope this helps and good luck with finding the solution that fits your needs!

Related

Programmaticaly spoofing an http script request in an iframe

I'm building a backend admin system which edits json files that control the look and feel of the main site. I want to add a 'preview' button before the user hits save. To do that, I want to use the main site, but instead of calling the actual json file in production, save a temp version of it and redirect this user's traffic for that file to the temp file - from the original site code.
i've considered both chrome pluggins, configuring iframe somehow or, in worst case scenario, grabbing the production front-end, parsing out the call to the prod json file and replacing with new temp json file. That is obviously not ideal as it would entail a lot of work and if anything changes on the prod site, this will have to be updated.
I would love your ideas!
Do you have access to the main site's source code? You could implement a preview option from the main site which accepts a GET parameter and uses a temporary JSON setting based on this GET parameter.
From the backend admin system's point of view, it's just a matter of adding the JSON as part of the ajax GET request.
Unfortunately though, there is no easy way of doing this if you don't have access to the main site's source code or if you can't reach out to whoever maintains that main site.
Your cleanest option might be to recreate the main site's look and feel instead and pass it off as a "preview".

How to create a file in IIS instead of returning a 404 error?

I'm writing an ASP.NET application and I'd like to have a folder that IIS uses as a normal web folder, but has a special handler when file-not-foun errors occur.
("http://example.com/Magic/" maps to "C:\example.com\Magic".)
Client requests http://example.com/Magic/1.jpg, but c:\example.com\1.jpg is missing. Instead of responding with a 404, some ASP C# is invoked, which builds itself 1.jpg and writes it in place. IIS looks again, finds 1.jpg, and returns it to client as if it were always there.
Later, another client also asks for http://example.com/Magic/1.jpg. Since IIS finds the file where it expects, it returns it to the client with all the caching and byte range requests that IIS allows when serving static files.
Even later, another client requests http://example.com/Magic/2.jpg. The same code as before is invoked to create 2.jpg, but the code decides theres no such file and so IIS returns a 404 error.
A few notes...
The point of doing it this way instead of having an ASPX supply the file every time is that IIS will serve a static file much better than an ASPX page will. IIS supports if-modified-since and byte-range requests and this way the ASP.NET module won't need to be loaded.
As admin, with this just-in-time file creation in place, I could occasionally clear out the 'Magic' folder and any files that turned out to be needed again would be recreated the first-time it is asked for again.
I imagine if two clients ask for the same URL at the same time, two copies of the file-create code would invoked for the same file. This would have to be robustly written to expect this possibility.
Answering why I'd want to do such a thing in comments:
Ensuring '1.jpg' is already present on the file server would be great, except the files are big and disk space is scarce. I'd want to delete those files once the client has used them, but there's no signal for the client to say "I'm done with this now". If I prematurely delete the file, I'd want to recreate the file when the client requests it again without generating a 404.
Using ASP.NET to generate the file on each request would superficially work, but ASP.NET isn't very good at serving up static files. The client makes extensive use of cache headers and requesting ranges of bytes and I'd have to reimplement all of that handling when IIS does the job already.
Custom error pages... After the custom code has run, can IIS be told to look again, find the file it was looking for, and then continue as if there never any error?

Login to asp.net site from another app, then receive filestream

I have an ASP.NET application with pages that use reportviewer. Can someone give me a hint on how to approach the following requirement:
I want to get the report as PDF file from the page, without user interaction. I know I can render the report to a filestream, but since there's no user opening it in a browser, I need to collect the filestream from another application that might run during the night.
There might be other approaches, like a webservice for example that could return the filestream to me, but this would also mean, I have to modify the setup of the datasources that the report receives it's data from. There are a lot of controls on the page, for supplying filter parameters. By using the page life cycle I can use what's already there.
I thought about wget, but haven't tried it yet, and I'm not sure how complicated logging in will be with cookies. I do have full control over the asp.net application though, so if I can modify something there to make it easier, I'd do it.
You can use the "WebClient" in .net application to get the response from the site.
Please refer the below link:
http://msdn.microsoft.com/en-us/library/system.net.webclient(v=vs.80).aspx

Application_AuthenticateRequest hit for all requests, including images and js files

In my MVC3 application, I'm using Application_AuthenticateRequest to create my custom user context and create the session. However, I notice that this is getting fired for every file per page request, including images, js, css, etc.
Is this the right method to do what I'm trying to do, or should I be doing this somewhere else (i.e. action filter)? Or, is this the right place, I just need to put some checks and/or configuration to ensure this method (or my block of code) is just executed for page requests instead of requests for static files?
I searched for a while trying to find the answer, and found one specific to IIS7, but this is happening for me on my ASP.NET dev server (debugging) on WinXP. Other than that, I couldn't find much, which leads me to think I may be way off on something here, possibly overlooking something simple.
Thanks in advance.
Jerad,
You are correct that you would be better off creating an action filter to handle your user context. You can decorate those controllers where the user context is required.
This is a better solution than using code to investigate the request, just so you can ignore particular requests.
counsellorben

Using uploadify as part of an asp.net webform?

I have a web form that requires users to fill out some information and upload an image.
What I don't understand:
If I use uploadify to select a file doesn't it upload it right away to the server? Is there a way to defer that until the user would click on a form submit button? Or at least not save it to the file system?
Most examples use a custom HttpHandler for uploading files, but my file upload is part of a form. Should I still use a HttpHandler for that?
Well, I'll try to answer all your many questions, one by one. But before anything, open the official documentation because I will rely on it for answers.
If I use Uploadify to select a file doesn't it upload it right away to the server?
As you can see on the first demo, you can have a anchor (or a button or anything) to trigger the upload start. The Uploadify don't upload nothing until it's done (if the property auto isn't true).
Is there a way to defer that until the user would click on a form submit button?
Like described above, yes. And it's the default way (since the default value of auto is false).
Or at least not save it to the file system?
While the button doesn't trigger the .uploadifyUpload() method, nothing goes to the server. But when the Uploadify starts sending, it will be handled by the server (with the HTTP Handler). The handler is the guy that save it to the file system.
Most examples use a custom HttpHandler for uploading files, but my file upload is part of a form. Should I still use a HttpHandler for that?
As described on the script property, you can point to any server-side language that will handle the HTTP Request containing the data. The HttpHandler is the right thing because it haven't any other processing before or after the code you write. It isn't hard. The official forum shows some samples. And here on StackOverflow we have many questions about it, like these: Getting Uploadify Working in C# and Uploadify not working with ASP.NET WebForms.
The server shouldn't save the file until it is actually actioned by a handler. The Uploadify is providing the UI control for the gathering of the files and feedback. There are some settable properties available to control whether you want the "Auto" upload behavior or not.
You still need some king of server side processing for saving the data and an HTTPHandler is the usual way to do it. The HTTPHandler just operates on what file information it gets from the browser as part of a multi-part form data.
Here is an answer that provides more information about Uploadify: Getting Uploadify Working in C#

Resources