限制用户上传包含特殊字符

时间:2015-08-19 06:41:44

标签: javascript c# asp.net file-upload

目前发生的事情是,每当用户上传包含某些特殊字符的文件时,例如%,&,#。它上传,下载时会出现问题。

所以现在我想要的是,每当用户开始上传文件时,它应该在 JAVASCRIPT 中给出警报消息,说明请上传没有特殊字符的文件,的空间

这是我的FileUpload代码: -

<td style="width: 10%;" class="field">
<asp:fileupload id="fileUpload" runat="server" width="100%" />
<div id="divSText" runat="server" style="display: none">
    <asp:textbox id="txtSText" runat="server" textmode="MultiLine" width="80%" rows="9">
                    </asp:textbox>
</div>

另见背后的代码: -

protected void btnSave_Click(object sender, EventArgs e)
{
    string Datafile = ""; HttpPostedFile PF_File; string Filename = "";
    if (HidFlag.Value == "Add")
    {
        if (Directory.Exists(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/")) == false)
        {
            Directory.CreateDirectory(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/"));
        }
        if (Directory.Exists(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value)) == false)
        {
            Directory.CreateDirectory(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value));
        }
        if (Directory.Exists(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value + "/" + ddlCategory.SelectedValue)) == false)
        {
            Directory.CreateDirectory(Server.MapPath("~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value + "/" + ddlCategory.SelectedValue));
        }
        if (fileUpload.PostedFile.FileName != "")
        {
            PF_File = fileUpload.PostedFile;
            Datafile = fileUpload.FileName;
            Filename = Datafile.Substring(Datafile.LastIndexOf("\\") + 1, Datafile.Length - Datafile.LastIndexOf("\\") - 1);
            PF_File.SaveAs(Server.MapPath((@"~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value + "/" + ddlCategory.SelectedValue + "/" + Filename)));

            string str_query = "insert into EMP_ATTACHED_DOCUMENTS(form_id, document_id, category_id, title, descriptions, file_name, file_path, pk1_value, delete_flag, creation_date, created_by) " +
                                      "values('" + Request.QueryString["form_id"] + "', '" + Request.QueryString["document_id"] + "', '" + ddlCategory.SelectedValue + "', '" +
                                       txtTitle.Text + "','" + txtDescription.Text + "','" + Filename + "','" +
                                      Server.MapPath((@"~/Payroll/Masters/FileAttachment/ESS/" + Hid_Empcode.Value + "/" + ddlCategory.SelectedValue + "/" + Filename)) + "','" +
                                      Request.QueryString["empcode"] + "', 'N', getdate(),'" + Request.QueryString["empcode"] + "' )";
            ObjPriDal.ExecuteNonQuery(str_query);
        }
    }
    else if (HidFlag.Value == "Edit")
    {
        ObjPriDal.ExecuteNonQuery("insert into EMP_ATTACHED_DOCUMENTS_H select getdate(), * From EMP_ATTACHED_DOCUMENTS where mkey=" + HidFileID.Value);
        ObjPriDal.ExecuteNonQuery("update EMP_ATTACHED_DOCUMENTS set title='" + txtTitle.Text + "', descriptions='" + txtDescription.Text + "' where mkey=" + HidFileID.Value);
    }
    ClientScript.RegisterStartupScript(this.GetType(), "CloseScript", "document.cookie = 'CAttachid=1'; window.open('FrmCrm_File_Attachment.aspx?form_id=" + Request.QueryString["form_id"] + "&document_id=" + Request.QueryString["document_id"] + "&category_id=" + Request.QueryString["category_id"] + "&empcode=" + Request.QueryString["empcode"] + "&mkey=" + Request.QueryString["mkey"] + "','_self');", true);
}

1 个答案:

答案 0 :(得分:2)

试试这个

<asp:fileupload id="fileUpload" runat="server" width="100%" />
<asp:Button Text="Save" ID="btnSave" runat="server" OnClientClick="javascript:addcheck();Validate();" />

javascript功能

<script type="text/javascript" language="javascript">
    function Validate()
            {

                var fileUpload= document.getElementById('<%= fileUpload.ClientID %>');                
                var myfile = fileUpload.value;               
                if(myfile.search(/[<>'\s+\"\/;`%]/)>0)
                {
                 alert('please upload the file without special characters and SPACES');
                 return false;
                }
                else
                {
                 alert('valid Format');
                 return true;
                }

            }
    </script>