仅限FileUpload图片扩展 [英] FileUpload Picture Extensions Only

查看:47
本文介绍了仅限FileUpload图片扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个Fileupload,我只想将其用于从工具箱创建的图像,但是在浏览时我只希望允许图片扩展名?

I have created an Fileupload that i only want to use for images that i have created from the toolbox but when browing i only want to allow picuture extensions? anyone that know how i could start?

推荐答案

您可以像这样获取上载的文件扩展名,然后验证或与您的扩展名预定义列表进行比较:

You can get uploaded file''s Extension like this and then validate or compare with your predefined list of extensions like this:

if (fileupload1.HasFile)
{
FileInfo finfo = new FileInfo(fileupload1.FileName);
string fileExtension = finfo.Extension.ToLower();
/*compare this with a list of acceptable extensions..
 if it contains in list then
 fileupload1.SaveAs(filename);
 else return with a message..*/

}



或者通过javascript,您可以尝试这样..



Or by javascript you can try like this..

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
  <script language="JavaScript">
      function CheckType() {
          var id = document.getElementById('fileupload1').value;

          if (id != '') {
              var myextensions = /(.jpg|.jpeg|.gif)


/i; 如果(myextensions .test(id)){ 警报('OK'); } 别的 { alert('无效的文件类型.') } } } < /script > < /head > < 正文 > < asp:FileUpload ID =" runat 服务器" onChange CheckType()" / > < /body > < /html >
/i; if (myextensions .test(id)) { alert('OK'); } else { alert('Invalid file type.') } } } </script> </head> <body> <asp:FileUpload ID="FileUpload1" runat="server" onChange="CheckType()" /> </body> </html>


这篇关于仅限FileUpload图片扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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