简体   繁体   中英

Ajax / iFrame / FileSystemObject Upload

I have built an uploader for my application but struggling on something.

Every 3 seconds my AJAX script makes a call to my ASP page, checkProgress.asp, to check how many files are in that folder during upload, it also checks if a txt file called complete.txt is in there.

When the count is done, it sends a response back to the AJAX script with something like "File 2 uploaded..." and 3 seconds later it will send "File 3 uploaded...", and so on. If the complete.txt file was found, it would return "Complete" instead of counting. This worked fine, once, and then didn't seem to perform properly after that. I get the "complete" message as I should but not getting the file count progress response.

I ran the checkProgress page manually with a new browser window to see why my progress panel was not updating with the progress, and noticed that the browser loading icon was just spinning, and when the upload finished, "Complete" popped up. So the AJAX call wasn't reaching the page to gather the count of files because it was busy, which confuses me, because all that page is doing is counting how many files are in a folder.

Can somebody suggest what I'm doing wrong? Is this just simply not going to happen while that folder is being added to?

Here is my AJAX script. This starts when the upload starts:

var upload1ProgressCheckInt = setInterval(function() {
    var postData = "token="+token;
    $.ajaxSetup ({ cache: false });

    $.ajax({ type : 'GET', url : 'ajax/checkProgress.asp',
    dataType : 'html', data : postData,
    success : function(data) {
        if (data == "Failed") {
            $('#upload1ProgressStatus').html('Error: Upload cancelled!');
            clearInterval(upload1ProgressCheckInt);
            // do stuff
        } else if (data == "Complete") {
            $('#upload1ProgressStatus').html('Success: Files uploaded');
            clearInterval(upload1ProgressCheckInt);
            // do stuff
        } else {
            $('#upload1ProgressStatus').html(data);
        }
    }
    }); // end ajax
}, 3000);

and this the checkProgress.asp page:

Set FSO = Server.CreateObject("Scripting.FileSystemObject")
If (FSO.FileExists(Server.MapPath("../files/photos/"&token_&"/complete.txt"))) = True Then
    Response.Write "Complete"
Else
    Set folder = FSO.GetFolder(Server.MapPath("../files/photos/"&token_&"/"))
    Set files = folder.Files
    fileCounter = files.Count
    Response.Write "File "&fileCounter&" uploaded..."
End If

...continuing from comments.

So here is how I understand it. Classic ASP assigns a worker thread to each session, because Classic ASP object are single threaded, and thus the only way to share them (stored in the Session object) between requests is to have a single thread for each session. Naturally that means exactly what you are seeing - all other requests are blocked until upload finishes.

Way to work around that would be to break out of the session. Unfortunately session cookie is HTTP-only (for security reasons), so I don't think there is a way to drop it from AJAX request.

You could make your IIS entry respond to another hostname and convert your AJAX request into JSONP request to that second hostname. I am not sure if there is a more graceful way to do it.

==================

EDIT: Actually I take back the part about cookies. Surely you can kill them by giving headers:{Cookie:""} to your ajax() call? Worth a try....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM