I'd like to get both C# and VB.NET sugestion.
I have an ASP.NET FileUpload control, which allows only image type. I used RegularExpressionValidator like below.
<asp:FileUpload ID="fuPhoto" runat="server" TabIndex="40" />
<asp:Regula开发者_如何学编程rExpressionValidator ID="RegularExpressionValidator3"
runat="server" ControlToValidate="fuPhoto" Display="Dynamic"
ErrorMessage="* You can only upload .jpg, .gif or .png image types."
ValidationExpression="^.*\.(jpg|JPG|jpeg|JPEG|gif|GIF|png|PNG)$">* You can only upload .jpg, .gif or .png image types.</asp:RegularExpressionValidator>
This method will only verify the file's extension, not its actual type. Once I receive the file, I want to examine its contents to determine what it really is, in this case image only.
How do I examine the file content being uploaded?
I think that regular expression is wrong because it prevents me uploading jpg's. In any case you can check the content type on the server using something like:
protected void Page_Load(object sender, EventArgs e)
{
foreach (string filename in Request.Files)
Response.Write("<p>" + Request.Files[filename].ContentType + "</p>");
}
Refer to http://en.wikipedia.org/wiki/Internet_media_type for valid types
精彩评论