Ajax文件上传控件验证使用javascript和ASP.NET无法正常工作 [英] Ajax file upload control validation not working properly using javascript and ASP.NET

查看:113
本文介绍了Ajax文件上传控件验证使用javascript和ASP.NET无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在asp.net中为ajax文件上传控件创建验证。基本上,如果用户没有上传两个图像(在单独的文件上传控件中),然后单击提交按钮,它将不会回发以保存在数据库中,并且将在fileupload控件下方显示一条消息以上载图像。但即使我已经上传了一个图像并再次单击提交按钮,它也不会回发并保存到数据库。



似乎它仍然返回一个值即使我已经上传了图片也是null。是否有其他方法来验证ajax文件上传控件以检查用户是否确实上传了图像?请帮助我解决这个问题。我在下面列出了实际代码。



这是aspx:



我是什么尝试过:



I tried creating a validation for ajax file upload control in asp.net. Basically if the user does not upload two images(which is in separate file upload control) and then clicks the submit button, it will not postback to save in database and a message will display below the fileupload control to upload an image. But even if i upload already an image and click the submit button again, it does not postback and save to database.

It seems like it is still returning a value of null even if i have already uploaded an image. Are there other ways to validate the ajax file upload control to check whether the user has really uploaded an image? Kindly help me on solving this one. I have included the actual codes below.

Here is the aspx:

What I have tried:

<asp:AjaxFileUpload ID="itemFileUpload1" runat="server" 
 OnUploadComplete="itemUploadImage1_Click" MaximumNumberOfFiles="1"/>
                      <p  id="itemImage1Validate"></p>





这里是javascript代码:





And here is the javascript code:

function checkForm(form) {
var errors = [];

if (form.itemFileUpload1.value == null) {
    errors[0] = "Please upload item image 1!";
}

if (form.itemFileUpload2.value == null) {
    errors[1] = "Please upload item image 2!";
}


if (errors.length > 0) {

    if (errors[0] != null) {
        document.getElementById("itemImage1Validate").innerHTML = errors[0];
    }

    if (errors[1] != null) {
        document.getElementById("itemImage2Validate").innerHTML = errors[1];
    }

    return false;
}

return true;
}

推荐答案

试试这个



try this

var itemsSelected = document.querySelector('[id


=itemFileUpload1]')。getElementsByClassName('ajax__fileupload_fileItemInfo');
if(itemsSelected.length == 0)
{
alert('请选择一个文件');
}
="itemFileUpload1"]').getElementsByClassName('ajax__fileupload_fileItemInfo'); if (itemsSelected.length == 0) { alert('Please choose a file'); }


这篇关于Ajax文件上传控件验证使用javascript和ASP.NET无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆