在asp.net中需要有关文件上传控制的帮助 [英] Need help on fileupload control in asp.net

查看:80
本文介绍了在asp.net中需要有关文件上传控制的帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有一个文件上传控件,可将excel文件上传到SQL Server数据库.

问题是当我单击浏览按钮时,它将打开openfile对话框并显示所有文件.但是我想要的是它应该仅显示.xls文件.

我试图在Web应用程序中实现openfiledialog控件(win窗体控件),但抛出STA线程异常.

如果不可能,我们能否仅显示过滤器中的所有文件.它显示了jpeg和gif fies.

请尽快帮助我.

Hi all

I have one file upload control which uploads the excel file to SQL server database.

the problem with this is when i click on browse button it will open openfile dialog and shows all files. But what i want is it should show only .xls files.

I have tried to implement openfiledialog control(win forms control) in web application but throwing STA thread exception.

if it is not possible can we show only all files in filter. it was showing jpeg and gif fies.

please help me on this ASAP.

推荐答案


不幸的是,您无法为文件浏览器对话框设置过滤器,但是可以连接一个JavaScript,该JavaScript将在文本框的内容更改时触发,然后验证扩展名.这样,您可以通知用户他/她做出了无效的选择.


Unfortunately you cannot set a filter for the file browser dialog, but you could hook up a JavaScript that would trigger when the contents of the text box has changed and then validate the extension. That way you could inform the user that he/she has made an invalid selection.

function checkFileExtension(elem) {
        var filePath = elem.value;
        if(filePath.indexOf(''.'') == -1)
            return false;
        
        var validExtensions = new Array();
        var ext = filePath.substring(filePath.lastIndexOf(''.'') + 1).toLowerCase();
    
        validExtensions[0] = ''jpg'';
        validExtensions[1] = ''jpeg'';
        validExtensions[2] = ''bmp'';
        validExtensions[3] = ''png'';
        validExtensions[4] = ''gif'';  
        validExtensions[5] = ''tif'';  
        validExtensions[6] = ''tiff'';
        validExtensions[7] = ''txt'';
        validExtensions[8] = ''doc'';
        validExtensions[9] = ''xls'';
        validExtensions[10] = ''pdf'';
    
        for(var i = 0; i < validExtensions.length; i++) {
            if(ext == validExtensions[i])
                return true;
        }
        alert(''The file extension '' + ext.toUpperCase() + '' is not allowed!'');
        return false;
    }  



并要调用它,请在您的Page_Load中添加以下行:



And to invoke this, add this line in your Page_Load:

FileUpload1.Attributes.Add("onchange", "return checkFileExtension(this);")




谢谢:)




Thanks :)



非常感谢您的友好答复.

幸运的是,我已经实现了此功能,只能选择excel文件,否则我将阻止用户提交该文件.

但是,如果您尝试在单击浏览器时在gmail中添加附件,它将在过滤器中仅显示所有文件选项.所以我的客户要求实施该选项.

在orkut上上传或更改照片时可以找到的相同功能.

如果您找到任何解决方案,请帮助我.

预先感谢.
Hi
Many thanks for your kind reply.

Fortunately i have already implemented this functionality to select only excel files other wise i am preventing users to submit that file.

But if you try to add attachment in gmail when click on browse it will show only all files option in filter. so my client asking to implement that option.

Same functionality you can find when you upload or change photo on orkut.

Please help me out if you find any solution for this.

Thanks in advance.


这篇关于在asp.net中需要有关文件上传控制的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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