如何在ASP.NET AJAX Control Toolkit中的AsyncFileUpload的文件浏览菜单中限制文件类型

时间:2022-01-18 03:17:57

I would like to restrict what they see in the file upload dialog, which is set to "All Files" by default. I understand how to validate that they only uploaded a certain file type, that is not the question here. I would just like to know how to default the file type in the file selection dialog.

我想限制他们在文件上传对话框中看到的内容,默认情况下设置为“所有文件”。我理解如何验证他们只上传了某种文件类型,这不是问题。我只想知道如何在文件选择对话框中默认文件类型。

Is there any way to change this to "PNG only" or "*.png"?

有没有办法将其更改为“仅限PNG”或“* .png”?

This is using AsyncFileUpload in the ASP.NET AJAX Control Toolkit.

这是在ASP.NET AJAX Control Toolkit中使用AsyncFileUpload。

3 个解决方案

#1


2  

The current version of the ajax control toolkit don't have this option.

当前版本的ajax控件工具包没有此选项。

But the good new is that you could get the source code and add a property do handle this.

但好消息是你可以获得源代码并添加属性来处理这个问题。

#2


10  

This one is working for me (Thanks DavRob for the inspiration).

这个对我有用(感谢DavRob的灵感)。

<cc1:AsyncFileUpload ID="FileUpload" runat="server" 
  OnClientUploadStarted="AssemblyFileUpload_Started" />

<script>
function AssemblyFileUpload_Started(sender, args) {
    var filename = args.get_fileName();
    var ext = filename.substring(filename.lastIndexOf(".") + 1);
    if (ext != 'png') {
        throw { 
            name:        "Invalid File Type", 
            level:       "Error", 
            message:     "Invalid File Type (Only .png)",
            htmlMessage: "Invalid File Type (Only .png)" 
        }
        return false;
    }
    return true;
}
</script>

#3


3  

You can use the OnClientUploadStart property on the control to fire a JavaScript function for validation, like this:

您可以使用控件上的OnClientUploadStart属性来激活JavaScript函数进行验证,如下所示:

<cc1:AsyncFileUpload ID="FileUpload" runat="server" 
  OnClientUploadStarted="checkExtension" />

Then have this script in your page or included:

然后在您的页面中包含此脚本或包括:

function checkExtension(sender, args) {
  var ext = args.get_fileName().substring(filename.lastIndexOf(".") + 1);
  if (ext != 'png') {
    args.set_cancel(true);                           //cancel upload
    args.set_errorMessage("File type must be .png"); //set error message
    return false;
  }
  return true;
}

In this case we're just using various bits of the client-side API to get/check the extension, returning false and stopping the upload/setting the error message (optional) if it's invalid.

在这种情况下,我们只是使用客户端API的各个位来获取/检查扩展,返回false并停止上传/设置错误消息(可选)如果它无效。

#1


2  

The current version of the ajax control toolkit don't have this option.

当前版本的ajax控件工具包没有此选项。

But the good new is that you could get the source code and add a property do handle this.

但好消息是你可以获得源代码并添加属性来处理这个问题。

#2


10  

This one is working for me (Thanks DavRob for the inspiration).

这个对我有用(感谢DavRob的灵感)。

<cc1:AsyncFileUpload ID="FileUpload" runat="server" 
  OnClientUploadStarted="AssemblyFileUpload_Started" />

<script>
function AssemblyFileUpload_Started(sender, args) {
    var filename = args.get_fileName();
    var ext = filename.substring(filename.lastIndexOf(".") + 1);
    if (ext != 'png') {
        throw { 
            name:        "Invalid File Type", 
            level:       "Error", 
            message:     "Invalid File Type (Only .png)",
            htmlMessage: "Invalid File Type (Only .png)" 
        }
        return false;
    }
    return true;
}
</script>

#3


3  

You can use the OnClientUploadStart property on the control to fire a JavaScript function for validation, like this:

您可以使用控件上的OnClientUploadStart属性来激活JavaScript函数进行验证,如下所示:

<cc1:AsyncFileUpload ID="FileUpload" runat="server" 
  OnClientUploadStarted="checkExtension" />

Then have this script in your page or included:

然后在您的页面中包含此脚本或包括:

function checkExtension(sender, args) {
  var ext = args.get_fileName().substring(filename.lastIndexOf(".") + 1);
  if (ext != 'png') {
    args.set_cancel(true);                           //cancel upload
    args.set_errorMessage("File type must be .png"); //set error message
    return false;
  }
  return true;
}

In this case we're just using various bits of the client-side API to get/check the extension, returning false and stopping the upload/setting the error message (optional) if it's invalid.

在这种情况下,我们只是使用客户端API的各个位来获取/检查扩展,返回false并停止上传/设置错误消息(可选)如果它无效。