uploading multiple files with restrictions

Last post 05-19-2008 7:03 PM by amereto2k. 7 replies.

Sort Posts:

  • uploading multiple files with restrictions

    05-16-2008, 9:21 PM
    • Loading...
    • amereto2k
    • Joined on 01-30-2006, 11:31 AM
    • Posts 195

    hi,

     

    i am using the HttpFileCollection class to upload multiple files on one page.

    What I want to add is code to restrict files that dont have a jpg or gif format to be uploaded.

    I have tried various ways link using getextension method but cant seem  to get it working.

    The code is included beow if anybody can spell out the necessary code be much appreciated

     

    Amereto

     

    CodeBehind Page

    using System;

    using System.Collections;

    using System.Configuration;

    using System.Data;

    using System.Linq;

    using System.Web;

    using System.Web.Security;

    using System.Web.UI;

    using System.Web.UI.HtmlControls;

    using System.Web.UI.WebControls;

    using System.Web.UI.WebControls.WebParts;

    using System.Xml.Linq;

    public partial class FU5 : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    }

     

    protected void Button1_Click(object sender, EventArgs e)

    {

    string filepath = "C:\\FileUpload\\images";

    HttpFileCollection uploadedFiles = Request.Files;

    for (int i = 0; i < uploadedFiles.Count; i++)

    {

    HttpPostedFile userPostedFile = uploadedFiles[i];

    try

    {

    if (userPostedFile.ContentLength > 0)

    {

    Label1.Text +=
    "<u>File #" + (i + 1) +

    "</u><br>";

    Label1.Text += "File Content Type: " +

    userPostedFile.ContentType + "<br>";

    Label1.Text += "File Size: " +

    userPostedFile.ContentLength + "kb<br>";

    Label1.Text += "File Name: " +userPostedFile.FileName + "<br>";

     

     

    userPostedFile.SaveAs(filepath +
    "\\" +

    System.IO.Path.GetFileName(userPostedFile.FileName));

    Label1.Text += "Location where saved: " +

    filepath + "\\" +

    System.IO.Path.GetFileName(userPostedFile.FileName) +"<p>";

     

    }

     

     

     

     

     

    }

    catch (Exception Ex)

    {

    Label1.Text +=
    "Error: <br>" + Ex.Message;

    }

     

     

     

     

    }

    }

     

    }

     

     ASPX Page

     

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title>Upload Files</title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <asp:FileUpload ID="FileUpload1" runat="server" />

    <br />

    <br />

    <asp:FileUpload ID="FileUpload2" runat="server" />

    <br />

    <br />

    <asp:FileUpload ID="FileUpload3" runat="server" />

    <br />

    <br />

    <br />

    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click"

    Text="Upload File" />&nbsp;<br />

    <br />

    <asp:Label ID="Label1" runat="server"></asp:Label>

    <br />

     

    </div>

    </form>

    </body>

    </html>

     

  • Re: uploading multiple files with restrictions

    05-16-2008, 11:16 PM
    • Loading...
    • dinesh_sp
    • Joined on 12-03-2007, 5:41 AM
    • Melbourne
    • Posts 409

     You can check extension by System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();-->return extension of uploaded file.

    Or can do same by regex in regex validator.

    But I would prefer checking serverside as it is more secure.

    Hope this helps 

  • Re: uploading multiple files with restrictions

    05-16-2008, 11:29 PM
    • Loading...
    • jchandra
    • Joined on 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

    Looks like time to do some client side scripting here since trying to detect this from the server require for at least some / all part of the content get transfered first and if any of them is a big file, you'll just have to wait a while before you can even validate, cancel or whatever.

    Here is an example of how to get the value of the control in javascript when a button is pressed. Put that inside a script tag on your html source at the bottom of the page

     

    1    window.onload = function() {
    2        document.getElementById("Button1").onclick = function() {
    3            var re = /.+\.(gif|jpg)$/i; //regex for testing if a string is ending with gif or jpg
    4            var u1 = document.getElementById("FileUpload1").value;
    5            var u2 = document.getElementById("FileUpload2").value;
    6            var u3 = document.getElementById("FileUpload3").value;
    7            
    8            //If all of these uploader contain the right combo, upload them
    9            if (re.test(u1) && re.test(u2) && re.test(u3))
    10           {
    11               return true;
    12           }
    13           else
    14           {
    15               alert("EEEK!! Only gif or jpg is allowed");
    16               return false;
    17           }
    18       }
    19   }
    20   
    
      
    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: uploading multiple files with restrictions

    05-16-2008, 11:45 PM
    • Loading...
    • jchandra
    • Joined on 05-15-2008, 9:36 AM
    • Jakarta, Indonesia
    • Posts 197

    dinesh_sp:

    But I would prefer checking serverside as it is more secure.

     

    Agree, you should recheck these on the server side, but you should also do client side checking just to prevent the waste of bandwidth. 

    Jimmy Chandra
    Blogging at Incoherent Rambling

    Mark this post as Answer if you think it helped you solve the problem.

  • Re: uploading multiple files with restrictions

    05-17-2008, 4:53 AM
    Answer
    • Loading...
    • Haissam
    • Joined on 10-05-2006, 2:25 AM
    • Beirut - Lebanon
    • Posts 4,682
    Haissam Abdul Malak
    MCAD.NET
    | Blog |
  • Re: uploading multiple files with restrictions

    05-17-2008, 4:54 AM
    • Loading...
    • amereto2k
    • Joined on 01-30-2006, 11:31 AM
    • Posts 195

    Hi thank you for the reply.

    the thing is I have 4 file upload controls on the page and just using fileupload1 is not enough.

    Therefore i am using the HttpPostedfile - and looping through to get each file.

    I still need to find the code that checks each one for jpg of gif and think i can use the getextension method

    but not sure where it all fits together in the code below?

     rgds

    amereto

     

     

  • Re: uploading multiple files with restrictions

    05-17-2008, 6:30 AM

     Hello Dear


    Try this code it will solve ur problem,

     

    code:

     Protected Sub AddFile_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles AddFile.Click
            Dim ext As String = System.IO.Path.GetExtension(FindFile.PostedFile.FileName)

            If Not FindFile.Value = "" Then
                If ext = ".gif" Or ext = ".jpeg" Or ext = ".png" Or ext = ".doc" Or ext = ".txt" Or ext = ".pdf" Then
                    If hif.Count <= 4 Then
                        hif.Add(FindFile)
                        ListBox1.Items.Add(System.IO.Path.GetFileName(FindFile.PostedFile.FileName))
                        Dim i As String = System.IO.Path.GetFileName(FindFile.PostedFile.FileName)
                    Else
                        ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myscript", "alert('Maximum File Limit is 5.');", True)
                    End If
                Else
                    ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myscript", "alert('Extension of the File is not valid...');", True)
                End If
            Else
                ScriptManager.RegisterStartupScript(Me, Me.GetType(), "myscript", "alert('Choose File to Add...');", True)
            End If
        End Sub
     

    ======================================
    Regard,
    Pradeep Sahoo
    Sr. Sofware Developer
    Don't forget to click "Mark as Answer" on the post that helped you.
  • Re: uploading multiple files with restrictions

    05-19-2008, 7:03 PM
    • Loading...
    • amereto2k
    • Joined on 01-30-2006, 11:31 AM
    • Posts 195

    Hi,

    do you have that in C sharp by any chance? Oven ready?

Page 1 of 1 (8 items)
Microsoft Communities
Page view counter