如何使用 jQuery 表单插件取消文件上传

发布于 2024-12-09 13:51:08 字数 192 浏览 6 评论 0原文

我正在使用 jQuery Forms 插件来执行异步文件上传。目前,用户中止上传的唯一方法是按浏览器上的“停止”按钮,这可能会导致其他 javascript 和 ajax 请求也停止。我想提供一个取消按钮,但我还没有找到任何方法来取消使用 API 进行的上传

是否有一种内置方法,或者至少是我可以使用的强大黑客,可以在上传时取消上传?

I'm using the jQuery Forms plugin to do an asynchronous file upload. Currently, the only way for the user to abort that upload is to press the "Stop" button on their browser, which may cause other javascript and ajax requests to stop as well. I'd like to provide a cancel button, but I haven't found any way to cancel an upload as it is happening using the API.

Is there a built-in way, or at least a robust hack I can use, to cancel the upload as it occurs?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独夜无伴 2024-12-16 13:51:08

ajaxForm 方法采用与 jQuery.ajax 相同的所有选项,因此可以捕获它在 beforeSend 中生成的虚拟 xhr处理程序。 xhr 有一个 abort 方法可以中止文件上传:

f.ajaxForm({
    beforeSend: function(xhr) {
        cancelBtn.click(xhr.abort);
    }});

The ajaxForm method takes all the same options that jQuery.ajax takes, so it is possible to capture the dummy xhr that it produces in a beforeSend handler. The xhr has an abort method that aborts the file upload:

f.ajaxForm({
    beforeSend: function(xhr) {
        cancelBtn.click(xhr.abort);
    }});
寂寞美少年 2024-12-16 13:51:08

自 2012 年 12 月起,可以直接访问 xhr:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');
....
$('#cancel').on('click', function(){
    xhr.abort();
});

我用它来取消 ajax 文件上传。

链接 - https://github.com/malsup/form/issues/221

Since Dec 2012 it is possible to access xhr directly:

var form = $('#myForm').ajaxSubmit({ ... });
var xhr = form.data('jqxhr');
....
$('#cancel').on('click', function(){
    xhr.abort();
});

I use it to cancel ajax file upload.

link - https://github.com/malsup/form/issues/221

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文