ASP.NET - Limit file upload available file types

后端 未结 10 1214
忘了有多久
忘了有多久 2020-12-13 14:28

I have added a file upload to my asp.net website. However, I want to limit the file types that user can select. For example, I only the user to select mp3 files. How can I a

相关标签:
10条回答
  • 2020-12-13 14:54

    As stated above, it's not possible out of the box.

    The simpler solution that I've found: use a RegularExpressionValidator to check the file extension. No need for JavaScript or external libraries. Of course, it only checks the extension, not the file content (you must use server-side code and inspect the bytes), and does not change anything to the file list displayed in the folder browser.

    <asp:RegularExpressionValidator ControlToValidate="FileUpload1" ValidationExpression="^.*\.(mp3|MP3)$" runat="server" />
    
    0 讨论(0)
  • 2020-12-13 14:54

    Use the following code js code for to only select the required file type which we want to select. IN the below example I want to select only zip file, On browse it only shows the zip file extension file name

      (function ($) {
                $.fn.acceptFileType = function (types) {
                    if (types == undefined) {
                        return true;
                    } else {
                        types = types.split(",")
                    }
                    this.each(function () {
                        $(this).bind("change", function () {
                            if (!$.inArray($(this).val().replace(/([\d\w.]+)(\.[a-z0-9]+)/i, '\2'), types)) {
                                $(this).val('');
                                return false;
                            }
                            return true;
                        });
                    });
                };
            })(jQuery);
            $(":file").acceptFileType(".zip");
            
            
            
             <input type="file" id="txtFileUploadGrid" runat="server" accept=".zip,application/octet-stream,application/zip,application/x-zip,application/x-zip-compressed" />
            
            
            
            

    0 讨论(0)
  • 2020-12-13 14:57

    There is no problem. Here it is!

    <asp:FileUpload ID="FileUpload1" runat="server" accept=".mp3"/>
    
    0 讨论(0)
  • 2020-12-13 14:59

    It's not possible with the FileUpload control in ASP.NET, but the following link may help:

    http://forums.asp.net/p/1136820/1817938.aspx

    0 讨论(0)
提交回复
热议问题