ASP。Net FileUpload控件與Regex驗證器回發問題

[英]ASP.Net FileUpload Control with Regex Validator postback problem


I'm trying to use a .Net FileUpload control along with a Regex Validator to limit filename to JPG, GIF, or PNG extensions. After postback, the filename is gone from the control (as expected), but this seems to cause the validator to fire and display its error text.

我正在嘗試使用。net FileUpload控件和Regex驗證器將文件名限制為JPG、GIF或PNG擴展名。在回發之后,文件名從控件(如預期)中消失,但這似乎會導致驗證器觸發並顯示其錯誤文本。

Can anyone suggest a fix, or a better way? Thank you!

誰能提出一個解決辦法,或者更好的辦法嗎?謝謝你!

4 个解决方案

#1


4  

Just use a custom validator with the following javascript function:

只需使用自定義驗證器和以下javascript函數:

function UploadFileCheck(source, arguments)
{
    var sFile = arguments.Value;
    arguments.IsValid = 
       ((sFile.endsWith('.jpg')) ||
        (sFile.endsWith('.jpeg')) ||
        (sFile.endsWith('.gif')) ||
        (sFile.endsWith('.png')));
}

The custom validator code:

自定義驗證器代碼:

<asp:CustomValidator ID="cvalAttachment" runat="server" ControlToValidate="upAttachment" SetFocusOnError="true" Text="*" ErrorMessage="Invalid: File Type (allowed types: jpg, jpeg, gif, png)" ClientValidationFunction="UploadFileCheck"></asp:CustomValidator>

That should be all your need to stop it client side before it gets posted back.

這應該是您在它被送回之前停止它的客戶端的所有需要。

#2


0  

Use a custom validator to do this check and call Page.IsValid in the method that handles the upload which will stop the processing of the upload if the file does not have the valid extension.

使用自定義驗證器執行此檢查和調用頁面。在處理上傳的方法中是有效的,如果文件沒有有效的擴展名,該方法將停止對上傳的處理。

#3


0  

JavaScript has no "endsWith", so use this code for the custom validator:

JavaScript沒有“endsWith”,因此使用此代碼進行自定義驗證:

function UploadFileCheck(source, arguments) { var sFile = arguments.Value; arguments.IsValid = ((sFile.match(/\.jpe?g$/i)) || (sFile.match(/\.gif$/i)) || (sFile.match(/\.bmp$/i)) || (sFile.match(/\.tif?f$/i)) || (sFile.match(/\.png$/i))); }

函數UploadFileCheck(源、參數){var sFile = arguments.Value;參數。IsValid =((sFile.match(g / \ .jpe嗎? /我美元))| |(sFile.match(美元/ \ gif / i))| |(sFile.match(bmp格式/ \ / i)美元)| |(sFile.match(f / \ .tif嗎? /我美元))| |(sFile.match(美元/ \ . png / i)));}

#4


0  

Using custom validator is way too long. Instead you can simply set the IsValid property of the RegularExpression in the click event of your upload button.

使用自定義驗證器太長了。相反,您可以在upload按鈕的單擊事件中設置正則表達式的IsValid屬性。

http://blogs.cametoofar.com/post/aspnet-regular-expression-validator-firing-after-postback.aspx

http://blogs.cametoofar.com/post/aspnet-regular-expression-validator-firing-after-postback.aspx


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/18/275045dcfb76a62e69efc5ed08552464.html



 
  © 2014-2022 ITdaan.com 联系我们: