文件上传:如何在不使用按钮的情况下调用函数? C# [英] File upload: how to call a function without using button? C#

查看:121
本文介绍了文件上传:如何在不使用按钮的情况下调用函数? C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在FileUpload控件中选择文件后,有没有办法直接调用函数?我们可以在不点击按钮的情况下完成吗?



选择文件后我想立即调用的函数是FileSelected()。之后,函数FileSelected()调用另一个名为CheckFileType()的函数。



我之后想要使用一个按钮来保存文件,但不是为了调用函数。



我的尝试:



 private void FileSelected()
{
if(FileUpload1.HasFile)
{
if(CheckFileType(FileUpload1.FileName))
{
String filePath =Cert /+ FileUpload1.FileName;
txtpath1.Text = filePath;
}
其他
{
Label1.Visible = true;
Label1.Text =您可以上传图片或PDF文件。;
}
}
}

private bool CheckFileType(string fileName)
{
string ext = Path.GetExtension(fileName);
开关(ext.ToLower())
{
case.gif:
case.png:
case.jpg:
case.jpeg:
case.pdf:
返回true;
默认值:
返回false;
}

}

解决方案

您可以使用fileupload控件的onchange事件



JavaScript Fileupload对象:onchange事件处理程序 - w3resource [ ^ ]



尽管如此,它不适用于你想做的事情。您选择的文件无法在回发之间记住,因此如果您以编程方式单击某个按钮,或者执行其他操作以将文件发送到服务器(如提交表单),那么您的代码将检查文件扩展名但是何时页面返回文件控件将再次为空,你无能为力。



因此,如果你想在上传之前检查文件的扩展名,你需要做它通过JavaScript。你仍然需要你的服务器代码,尽管恶意的人可以绕过你的javascript。



如果你谷歌有各种各样的做法,但这里有一些



使用html5文件验证文件上传api - BinaryTides [ ^ ]



检查文件大小&上传前的扩展使用jQuery |所有PHP技巧 [ ^ ]

Is there any way to call a function directly after selecting a file in the FileUpload control? Can we do it without having to click a button?

The function I want to call immediately after selecting the file is FileSelected(). After that the function FileSelected() calls another function called CheckFileType().

I do want to use a button afterwards to save the file, but not for calling the function.

What I have tried:

private void FileSelected()
{
    if (FileUpload1.HasFile)
    {
        if (CheckFileType(FileUpload1.FileName))
        {
            String filePath = "Cert/" + FileUpload1.FileName;
            txtpath1.Text = filePath;
        }
        else
        {
            Label1.Visible = true;
            Label1.Text = "You can upload ony images or PDF files.";
        }
    }
}

private bool CheckFileType(string fileName)
{
    string ext = Path.GetExtension(fileName);
    switch (ext.ToLower())
    {
        case ".gif":
        case ".png":
        case ".jpg":
        case ".jpeg":
        case ".pdf":
            return true;
        default:
            return false;
    }

}

解决方案

You can use the onchange event of the fileupload control

JavaScript Fileupload Object : onchange Event handlers - w3resource[^]

It's not going to work for what you want to do though. The file you have selected can't be "remembered" between postbacks so if you click a button programatically, or do anything else to send the file to the server like submit the form, then your code will check the file extension but when the page returns the file control will be empty again and there is nothing you can do about that.

So if you want to check the file's extension before you upload you need to do it via javascript. You still need your server-code as well though as malicious people can bypass your javascript.

There are various of doing that if you google but here are some

Validate file upload with html5 file api – BinaryTides[^]

Check File Size & Extension Before Uploading Using jQuery | All PHP Tricks[^]


这篇关于文件上传:如何在不使用按钮的情况下调用函数? C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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