use*_*290 3 javascript jquery file-upload button
我有一个表单,其中包含表单中的文件输入,更重要的是一个取消按钮,它应该取消文件上传:
var $fileImage = $("<form action='imageupload.php' method='post' class='imageuploadform' ...><label>" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='button' name='cancelImageBtn' class='cancelimage' value='Cancel' /></label>" +
"</p></form>");
Run Code Online (Sandbox Code Playgroud)
现在我已经完成了将文件上传到服务器的所有工作。但我有一个问题。
我的问题是我不知道如何取消和上传。目前,如果用户单击“取消”按钮,则什么也不会发生。我想要发生的是,如果用户单击“取消”按钮,那么它将导航到“stopImageUpload()”函数并停止文件上传。但是我该怎么做呢?
下面是我尝试创建取消按钮功能,但是当单击“取消”按钮时,没有任何反应。
$(".cancelimage").on("click", function(event) {
console.log("clicked");
event.preventDefault();
stopImageUpload(success);
});
Run Code Online (Sandbox Code Playgroud)
下面是完整的代码,显示了取消按钮功能的位置以及开始上传和停止上传文件的功能:
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
sourceImageForm = imageuploadform;
$(".cancelimage").on("click", function(event) {
console.log("clicked");
event.preventDefault();
stopImageUpload(success);
});
return true;
}
function stopImageUpload(success){
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');
return true;
}
Run Code Online (Sandbox Code Playgroud)
这是可能借给这里的人的答案。(不完全回答问题,因为它已经太旧了)
我将回答您如何实现取消上传,或更多取消 ajax 请求。这在很多应用程序中都可能需要。
您可以使用取消令牌取消请求。
axios 取消令牌 API 基于已撤销的可取消承诺提案。
您可以使用 CancelToken.source 工厂创建取消令牌,如下所示:
const CancelToken = axios.CancelToken;
const source = CancelToken.source();
axios.get('/user/12345', {
cancelToken: source.token
}).catch(function (thrown) {
if (axios.isCancel(thrown)) {
console.log('Request canceled', thrown.message);
} else {
// handle error
}
});
axios.post('/user/12345', {
name: 'new name'
}, {
cancelToken: source.token
})
Run Code Online (Sandbox Code Playgroud)
并触发取消
// cancel the request (the message parameter is optional)
source.cancel('Operation canceled by the user.');
Run Code Online (Sandbox Code Playgroud)
您还可以通过将执行器函数传递给 CancelToken 构造函数来创建取消令牌:
const CancelToken = axios.CancelToken;
let cancel;
axios.get('/user/12345', {
cancelToken: new CancelToken(function executor(c) {
// An executor function receives a cancel function as a parameter
cancel = c;
})
});
// cancel the request
cancel();
Note: you can cancel several requests with the same cancel token.
Run Code Online (Sandbox Code Playgroud)
文档https://github.com/axios/axios#cancellation
我们使用xhr 对象的abort()方法。
var xhr = new XMLHttpRequest(),
method = "GET",
url = "https://developer.mozilla.org/";
xhr.open(method, url, true);
xhr.send();
if (OH_NOES_WE_NEED_TO_CANCEL_RIGHT_NOW_OR_ELSE) {
xhr.abort();
}
Run Code Online (Sandbox Code Playgroud)
doc https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/abort
小智 5
有一个可能的解决方案。你可以这样做:
$("#cancel").click(function(){
$("#inputfile").remove();
$("#containerof_inputfile").append("<input type=\"file\" id=\"inputfile\"/>");
});
Run Code Online (Sandbox Code Playgroud)