Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use a RegularExpressionValidator to validate if the user tries to upload jpeg-files or not:</p> <pre><code>&lt;asp:FileUpload ID="FileUpload1" runat="server" /&gt;&lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload File" /&gt;&amp;nbsp;&lt;br /&gt; &lt;br /&gt; &lt;asp:Label ID="Label1" runat="server"&gt;&lt;/asp:Label&gt; &lt;asp:RegularExpressionValidator id="RegularExpressionValidator1" runat="server" ErrorMessage="Only jpeg files are allowed!" ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*)) +(.jpg|.JPG|.jpeg|.JPEG)$" ControlToValidate="FileUpload1"&gt;&lt;/asp:RegularExpressionValidator&gt; &lt;br /&gt; &lt;asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="This is a required field!" ControlToValidate="FileUpload1"&gt;&lt;/asp:RequiredFieldValidator&gt; </code></pre> <p>on serverside:</p> <pre><code> protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName); if (fileExt == ".jpeg" || fileExt == ".jpg") { //do what you want with this file } else { Label1.Text = "Only .jpeg files allowed!"; } } else { Label1.Text = "You have not specified a file."; } } </code></pre> <p>You should know that any user could change the extension f.e. from .exe to .jpg. The only way i know to check for the real file-type would be to use function from <code>Urlmon.dll</code>. Have a look at this SO-question if you want further informations: <a href="https://stackoverflow.com/questions/58510/using-net-how-can-you-find-the-mime-type-of-a-file-based-on-the-file-signature">Using .NET, how can you find the mime type of a file based on the file signature not the extension</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload