简体   繁体   English

Ajax / iFrame / FileSystemObject上传

[英]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. 我的AJAX脚本每隔3秒就会调用我的ASP页面checkProgress.asp,以检查上传期间该文件夹中有多少文件,它还会检查其中是否存在一个名为complete.txt的txt文件。

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. 计数完成后,它会使用诸如“文件2上传...”之类的内容将响应发送回AJAX脚本,三秒钟后它将发送“文件3上传...”,依此类推。 If the complete.txt file was found, it would return "Complete" instead of counting. 如果找到complete.txt文件,它将返回“ Complete”而不是计数。 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. 我使用新的浏览器窗口手动运行checkProgress页面,以查看为什么进度面板未随进度更新,并注意到浏览器加载图标只是在旋转,并且上传完成后,弹出“完成”。 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. 因此,AJAX调用未到达页面以收集文件计数,因为它很忙,这使我感到困惑,因为该页面所做的所有事情都是在计算一个文件夹中有多少个文件。

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. 这是我的AJAX脚本。 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: 这是checkProgress.asp页:

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. Classic ASP为每个会话分配一个工作线程,因为Classic ASP对象是单线程的,因此在请求之间共享它们(存储在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. 不幸的是,会话cookie仅限于HTTP(出于安全原因),因此我认为没有办法将其从AJAX请求中删除。

You could make your IIS entry respond to another hostname and convert your AJAX request into JSONP request to that second hostname. 您可以使IIS条目响应另一个主机名,然后将AJAX请求转换为该第二个主机名的JSONP请求。 I am not sure if there is a more graceful way to do it. 我不确定是否有更优雅的方法。

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

EDIT: Actually I take back the part about cookies. 编辑:实际上,我收回了有关cookie的部分。 Surely you can kill them by giving headers:{Cookie:""} to your ajax() call? 您肯定可以通过给ajax()调用加上headers:{Cookie:""}来杀死它们? Worth a try.... 值得一试....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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