Link to home
Start Free TrialLog in
Avatar of Jonathan Mori
Jonathan MoriFlag for United States of America

asked on

FileUploadControl RegularExpressionValidator to only accepts images, dwg, and pdf and exclude ampersands

Hi, I am trying to figure out a way to limit the fileupload control to only accept certain types of files. These include jpg, gif, png, pdf, and dwg. Also To prevent ampersands in the filename.
I am having trouble getting the regular expression correct.
<asp:RegularExpressionValidator id="FileUpLoadValidator" runat="server" 
                ErrorMessage="Please Only Upload PDF, Image, or DWG files." 
                ValidationExpression="(?:\\\w+)+\.(?:jpg|JPG|gif|GIF|PDF|pdf|DWG|dwg)$/\.(?:jpg|gif|pdf|dwg)$/i"
                ControlToValidate="DocUpload">
</asp:RegularExpressionValidator>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Stephan
Stephan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jonathan Mori

ASKER

Hi, that worked excellently! It caused me to run into a slight other problem though. I have it set up so that it uploads the file, and then inserts a link and description of the file into the database. This is allowing the upload fine, but when I try the database insert It is not allowing me because of no file in the uploader. Could this be solved by viewstates?

protected void DocUploadBtn_Click(object sender, EventArgs e)
    {
        ContentPlaceHolder PCSubmit = this.Master.FindControl("MainContent") as ContentPlaceHolder;
        string strPath;
        string strPath2;
        FileUpload DocUpload1 = (FileUpload)PCSubmit.FindControl("DocUpload");
        if (DocUpload1.HasFile)
        {
            strPath = (Server.MapPath("./GIS/DOCUMENTS/") + Path.GetFileName(DocUpload1.PostedFile.FileName));
            strPath2 = (Server.MapPath("./GIS/DOCUMENTS/") + DocUpload1.PostedFile.FileName);
            TextBox DocLocation1 = (TextBox)PCSubmit.FindControl("DocLocation");
            DocLocation1.Text = String.Format("http://penner.gis.com/shell/GIS/DOCUMENTS/{0}", strPath2.Substring(strPath2.LastIndexOf("\\") + 1));
            DocUpload1.SaveAs(strPath);
        }
    }

rotected void DocInsert_Click(object sender, EventArgs e)
    {
        //Open Both SQL Connections
        SqlConnection conn = new SqlConnection(GetConnectionString());
        SqlCommand insert1 = new SqlCommand("INSERT INTO DOCUMENTS_NV (SITE_NAME,DOC_DESCRIPT,DOCPATH) VALUES(@SITE_NAME,@DOC_DESCRIPT,@DOCPATH)", conn);
        int intResult = 0;
        conn.Open();

        //insert code
        ContentPlaceHolder DOCSubmit = this.Master.FindControl("MainContent") as ContentPlaceHolder;
        DropDownList Proj_NameDDL1 = DOCSubmit.FindControl("WELL_NAMEDDL") as DropDownList;

        string ddlProjName = Proj_NameDDL1.SelectedValue;
        insert1.Parameters.Add("@SITE_NAME", SqlDbType.NVarChar, 50, "SITE_NAME").Value = ddlProjName;

        TextBox DOC_DESCTextBoxMK = DOCSubmit.FindControl("DocDescTxtBox") as TextBox;
        if (DOC_DESCTextBoxMK != null)
        {
            string txtBoxDOCDESC = DOC_DESCTextBoxMK.Text.Trim();
            insert1.Parameters.Add("@DOC_DESCRIPT", SqlDbType.NVarChar, 75, "DOC_DESCRIPT").Value = Server.HtmlEncode(txtBoxDOCDESC);
        }

        TextBox DocLocationMK = DOCSubmit.FindControl("DocLocation") as TextBox;
        if (DocLocationMK != null)
        {
            string txtBoxDOCPATH = DocLocationMK.Text.Trim();
            insert1.Parameters.Add("@DOCPATH", SqlDbType.NVarChar, 250, "DOCPATH").Value = Server.HtmlEncode(txtBoxDOCPATH);
        }

        try
        {
            intResult = Convert.ToInt32(insert1.ExecuteNonQuery());

            if (intResult != 0)
            {
                Label MsgLabel = DOCSubmit.FindControl("DocMsg") as Label;
                MsgLabel.Text = "Records Entered and Document Uploaded.";
            }
            else
            {
                Label MsgLabel = DOCSubmit.FindControl("DocMsg") as Label;
                MsgLabel.Text = "No Recoreds Entered";
            }
        }
        catch (SqlException ex)
        {
            Label MsgLabel = DOCSubmit.FindControl("DocMsg") as Label;
            MsgLabel.Text = ex.Message;
        }
        finally
        {
            conn.Close();
        }
        
    }

Open in new window

Are you trying first to upload the file and when the response is back to the client, another option is available to insert it into the database?

If so, uploads will not be set in a viewstate, it only posts the data to the webserver.

If you want to do this, you could remember the file location path and name and when the second button is fired, you read the file for the information you need, or you have to set 1 button for uploading and saving within 1 event.

You could separate it into 2 methods that is triggered within 1 event.

I'll hope this is clear to you.
I've requested that this question be closed as follows:

Accepted answer: 200 points for stephanonline's comment #37569154
Assisted answer: 0 points for GravitaZ24's comment #37572504
Assisted answer: 200 points for stephanonline's comment #37572573

for the following reason:

Thank you, Got it going perfectly!
The answer I gave is the correct answer to the question. Why is the comment from the code a part of the solution if I gave a full solution to the question?
I apologize, Both comments helped me so I just split the points.