我有文件上传控件
<ajaxToolkit:AsyncFileUpload ClientIDMode="Static" name="aa[]" BackColor="Azure"
ForeColor="Black" OnClientUploadError="uploadError" OnClientUploadStarted="abc"
multiple="multiple" OnClientUploadComplete="uploadComplete" runat="server" ID="AsyncFileUpload1"
Width="400px" CompleteBackColor="White" UploadingBackColor="#CCFFFF" OnUploadedComplete="AsyncFileUpload1_UploadedComplete" />
我想从fileupload控件中删除特定文件。
假设我要删除第二个文件,所以我想实现类似这样的东西
fileupload1.files[1].remove()
是否可以在fileupload控件中使用?
我想在javascript中实现它
答案 0 :(得分:0)
如果使用FileReader,则可以使用JavaScript,然后添加普通文件输入更改事件侦听器,然后从更改事件循环遍历每个文件,过滤所需的验证,将有效文件推送到单独的数组中 使用FileReader API读取文件本地提交有效,处理过的文件到服务器,如下代码示例:
var validatedFiles = [];
$("#fileToUpload").on("change", function (event) {
var files = event.originalEvent.target.files;
files.forEach(function (file) {
if (file.name.matches(/something.txt/)) {
validatedFiles.push(file); // Simplest case
} else {
/* do something else */
}
});
});
validatedFiles.forEach(function (file) {
if (file.name.matches(/something.txt/)) { // You could also do more complicated validation after processing the file client side
var reader = new FileReader();
// Setup listener
reader.onload = (function (processedFile) {
return function (e) {
var fileData = { name : processedFile.name, fileData : e.target.result };
// Submit individual file to server
$.post("/your/url/here", fileData);
// or add to list to submit as group later
validatedFiles.push(fileData);
};
})(file);
// Process file
reader.readAsDataURL(file);
} else {
/* still do something else */
}
});
希望这有帮助