【问题标题】:How to restrict the other format files from Displaying while browsing with File Upload for Selecting a File to Upload?使用文件上传浏览选择要上传的文件时,如何限制显示其他格式文件?
【发布时间】:2013-05-02 09:44:00
【问题描述】:

在我的项目中,我在“文件上传”期间上传 Doc 或 PDF 文件供用户参考,浏览所有文件,包括 Doc 和 PDF 文件正在显示以进行选择,因此我在代码中使用 IF 条件来检查是否选择的文件是 DOC 或 PDF,但我只想显示 DOC 和 PDF 文件以供选择以丢弃我的代码是的 IF 语句,

        if (FileUpload1.HasFile != false)
        {
            // Read the file and convert it to Byte Array

            string filePath = FileUpload1.PostedFile.FileName;

            int size = FileUpload1.PostedFile.ContentLength;

            string filename = Path.GetFileName(filePath);

            string ext = Path.GetExtension(filename);

            string contenttype = String.Empty;

            int bufferSize = 1;
            byte[] buffer = new byte[bufferSize];

            //Set the contenttype based on File Extension

            switch (ext)
            {

                case ".doc":

                    contenttype = "application/vnd.ms-word";

                    break;
                case ".docx":

                    contenttype = "application/vnd.ms-word";

                    break;

                case ".pdf":

                    contenttype = "application/pdf";

                    break;

            }
            if (size <= 5242880)
            {
                if (contenttype != String.Empty && ext == ".doc" || ext == ".docx" || ext == ".pdf")
                {

                    Stream fs = FileUpload1.PostedFile.InputStream;

                    BinaryReader br = new BinaryReader(fs);

                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    string fname = @"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\FileUpload";
                    Directory.CreateDirectory(fname);
                    string strFullFilename = @"E:\Rajesh_Kumar\Application\Valuation\ExamManagement\ExamManagement\FileUpload\" + FileUpload1.FileName;
                    //SqlCommand   qry = new SqlCommand("select Filepath from answerkey");
                     FileInfo file = new FileInfo(strFullFilename);
                    //FileInfo file1 = new FileInfo(qry);
                    fname = Path.Combine(fname, strFullFilename);
                    if(file.Exists)
                    {
                        lblMessage.Visible = true;
                        lblMessage.ForeColor = System.Drawing.Color.Red;

                        lblMessage.Text = "File - ''"+filename+"'' - already Exists in the Database !!!";
                        FileUpload1.Focus();
                   }
                    else
                    {


                        //insert the file into database
                        string strQuery = "insert into answerkey(Filename, Type, Data,Filepath)" + " values (@Filename, @Type, @Data,@Filepath)";

                        SqlCommand cmd = new SqlCommand(strQuery);

                        cmd.Parameters.Add("@Filename", SqlDbType.VarChar).Value = filename;

                        cmd.Parameters.Add("@Type", SqlDbType.VarChar).Value = contenttype;

                        cmd.Parameters.Add("@Data", SqlDbType.Binary).Value = bytes;

                        cmd.Parameters.Add("@Filepath", SqlDbType.VarChar).Value = filePath;

                        InsertUpdateData(cmd);
                        File.Copy(filePath, strFullFilename, true);
                       lblMessage.Visible = true;

                        lblMessage.ForeColor = System.Drawing.Color.Green;

                        string filename1 = Path.GetFileNameWithoutExtension(filename);

                        lblMessage.Text =  "File -      '' " + filename1 + " ''  - of Size - ''" + size + " bytes''  - has been Uploaded Successfully";


                    }
                }
                else
                {
                    lblMessage.Visible = true;
                    lblMessage.ForeColor = System.Drawing.Color.Red;

                    lblMessage.Text = "File format not recognised.Upload Word/PDF formats Only";
                    FileUpload1.Focus();


                }


            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.ForeColor = System.Drawing.Color.Red;

                lblMessage.Text = "File Size is larger than 5 MB !!!";
                FileUpload1.Focus();
            }

        }
        else
        {
            lblMessage.Visible = true;
            lblMessage.ForeColor = System.Drawing.Color.Red;

            lblMessage.Text = "Please Select a File !!!";
            FileUpload1.Focus();

        }


        }

在此先感谢...

【问题讨论】:

  • 问题是什么?
  • @Obama 限制文件上传仅显示要上传的文件的选定格式而不是显示所有文件

标签: c# sql-server


【解决方案1】:

使用正则表达式验证器:

<asp:RegularExpressionValidator
                id="RegularExpressionValidator1" runat="server"
                ErrorMessage="Only Word and PDF files are allowed!"
                ValidationExpression="^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))(.pdf|.PDF|.doc|.DOC|.docx|.DOCX)$"
                ControlToValidate="FileUpload1" CssClass="text-red"></asp:RegularExpressionValidator>

希望这会有所帮助:)

【讨论】:

  • @Sweeety 正则表达式验证器与代码中的 IF 条件执行相同的操作,我不希望我只想限制其他格式文件在通过文件上传浏览以上传文件时显示.
  • 嗨拉杰什!在此处查看此链接 VB Code 或此处 C# Code。你也是在做web项目还是windows项目??
  • 您是否尝试过我在之前评论中附加的链接??
【解决方案2】:

对于网络,您不能这样做。

如需解决方法,您可以查看链接:
How to restrict file type in FileUpload control

【讨论】:

  • 在其中一个 Web 项目中它正在运行,但对于此页面它不起作用。
猜你喜欢
  • 2016-10-05
  • 2015-11-20
  • 2012-07-16
  • 2021-10-31
  • 1970-01-01
  • 2013-12-19
  • 1970-01-01
  • 1970-01-01
  • 2011-11-11
相关资源
最近更新 更多