简体   繁体   English

用jQuery在代码后面调用ASP.NET FileUpload函数

[英]Calling an ASP.NET FileUpload function in code behind with jquery

I'm using the ASP.NET FileUpload control. 我正在使用ASP.NET FileUpload控件。

To upload a file, a user must: 要上传文件,用户必须:

  1. Browse to select a file from his/her device, 浏览以从他/她的设备中选择一个文件,
  2. Click the upload button to upload the file to the server. 单击上载按钮将文件上载到服务器。

Is it possible to skip step 2 (the Upload button) and call the 'uploadFile_Click' function on the server through a jquery (postback) once a file is selected? 一旦选择了文件,是否可以跳过第2步(“上传”按钮)并通过jquery(回发)调用服务器上的“ uploadFile_Click”功能? I'm trying the following method but I need some help to finalize it. 我正在尝试以下方法,但需要一些帮助才能完成。

ASPX PAGE ASPX页面

<asp:FileUpload runat="server" ID="UploadImages" />
<asp:CustomValidator ID="CustomValidator1" runat="server" ControlToValidate="UploadImages" ClientValidationFunction="ValidateUpload" />

CODE BEHIND 后面的代码

protected void uploadFile_Click(object sender, EventArgs e)
{
// Upload code
}

JQUERY JQUERY

function ValidateUpload() {
var FileUpload_function = document.getElementById('UploadImages');

        if (FileUpload_function.value == '') {
            return false;
        }
        else {

           ** Here I want to trigger a postback to call the uploadFile_Click. Is that possible? **
        }
        return true;

    }

Some (most?) browsers will not allow you to call the click event on an input HTML element if it is of type file (ie. an asp:fileupload control). 某些(大多数?)浏览器不允许您在input HTML元素上使用file类型(例如,asp:fileupload控件)调用click事件。 It is considered a security risk. 它被认为是安全风险。

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

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