Chris
Chris

Reputation: 7621

Wait for file to exist in ASP.NET

I have an ASP.NET page, where the user is able to trigger a console app on our server, which generates a file. What I want is the user to see a message that states 'Requesting file', then as soon as the file becomes available (i.e. File.Exists for the file returns true), the Requesting file message will change to a link of the file.

Is this easy to accomplish in ASP.NET?

Upvotes: 0

Views: 722

Answers (4)

SShebly
SShebly

Reputation: 2073

In you web page implement a JSON call to a certain WebMethod which checks if the file has be been generated or not.

you can add your message in the calling function and clear it in the complete event, where you can create the link to the file also

function ddlLeaveChanged(sender, e) {


if (leaveTypeId != '-1' && dateFrom != '' && leaveStatusId != '-1') {
     $('#lblMessage').show();
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        url: WebServiceUrl + 'YourMethod',

        success: FunctionSuccedded,
        error: FunctionFailed
    });
}

function FunctionsSuccedded(result, e) {
if (result) { $('#lblMessage').hide(); }

}

Upvotes: 2

Ta01
Ta01

Reputation: 31630

If you want to avoid file systems all together you could key off a database field as well, i.e. when the linked is clicked an entry is made into a database table of some sort that signifies file creation pending, and then periodically check if the status has been updated. The server side code would update that same key and have a url in the datastore as well, so you could just check for the status of file created and the link the datastore. This would also result in you having a history of file creation, which you could purge as you saw fit, so from an ASP.NET perspective you would only be relying on data access code to determine if your file was created.

Upvotes: 1

Curtis
Curtis

Reputation: 103388

This can be achieved using a combination of:

A javascript function can be triggered every x seconds (using setInterval) to trigger a Web Method using AJAX to check whether the file exists.

Check the links I've provided above. These should be all you need to get this working.

Upvotes: 1

Jon
Jon

Reputation: 437664

Not much different in ASP.NET vs any other platform. You can go about it like this:

  1. Make AJAX call when the triggering link is clicked.
  2. Server-side code that handles the call launches an external process to generate the file, and waits for it to complete.
  3. After checking that the file is indeed there, reply to the AJAX call with a meaningful message (i.e. one that contains enough information to build a link to the file).

Upvotes: 0

Related Questions