0

我正在尝试上传文件。用户可以上传文件,但他/她可以上传图片而不是文件,我想限制这个如何应用条件这是我的上传代码

            if (FileUploadControl.PostedFile != null && 
             FileUploadControl.PostedFile.ContentLength 
            > 0)
        {
            if
                (FileUploadControl.FileContent.Length < 100000)
            {
                string filename = 
                Path.GetFileName(FileUploadControl.PostedFile.FileName);
                string folder = Server.MapPath("~/Docfiles/");
                Directory.CreateDirectory(folder);
                FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));
                try
                {
                    cc.upload1(Txt_docde.Value, txt_dname.Value, 
              FileUploadControl.FileName, Convert.ToInt32(Docdrop.SelectedValue),
                       Convert.ToInt32(DropDownList2.SelectedValue), 
              Convert.ToString(Session["Login2"]),Convert.ToInt32(Session["UserID"]));
                    StatusLabel.ForeColor = System.Drawing.Color.Green;
                    //StatusLabel.ForeColor = System.Drawing.FontStyle.Bold;
                    StatusLabel.Text = "Success";
                }
                catch
                {
                    StatusLabel.ForeColor = System.Drawing.Color.Red;
                    Label2.Text = "Failed";


                }
            }
                else
            {
                 StatusLabel.ForeColor = System.Drawing.Color.Red;
                            Label2.Text = "File Size to big";
            }
        }
4

4 回答 4

3

制作您想要允许的扩展名的通用列表,然后检查您尝试上传的文件是否满足该扩展名要求。

var allowedExtensions = new string[] { "doc", "docx", "pdf" };
var extension = Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(".", "");

if (allowedExtensions.Contains(extension))
{
    // Good to go
}

这是给你的完整代码

if (FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.ContentLength > 0)
{
    var allowedExtensions = new string[] { "doc", "docx", "pdf" };
    var extension = Path.GetExtension(FileUploadControl.PostedFile.FileName).ToLower().Replace(".", "");

    if (FileUploadControl.FileContent.Length < 100000 && allowedExtensions.Contains(extension))
    {
        string filename = 
        Path.GetFileName(FileUploadControl.PostedFile.FileName);
        string folder = Server.MapPath("~/Docfiles/");
        Directory.CreateDirectory(folder);
        FileUploadControl.PostedFile.SaveAs(Path.Combine(folder, filename));

        try
        {
            cc.upload1(Txt_docde.Value, txt_dname.Value, FileUploadControl.FileName, Convert.ToInt32(Docdrop.SelectedValue), Convert.ToInt32(DropDownList2.SelectedValue),  Convert.ToString(Session["Login2"]),Convert.ToInt32(Session["UserID"]));
            StatusLabel.ForeColor = System.Drawing.Color.Green;
            StatusLabel.Text = "Success";
        }
        catch
        {
            StatusLabel.ForeColor = System.Drawing.Color.Red;
            Label2.Text = "Failed";
        }
    }
    else
    {
         StatusLabel.ForeColor = System.Drawing.Color.Red;
         Label2.Text = "File Size to big";
    }
}
于 2013-11-13T17:50:36.007 回答
1

尝试这样的事情来验证您感兴趣的文件类型后缀:

if (string.Equals(fileExt, ".pdf", StringComparison.OrdinalIgnoreCase)) {...}
于 2013-11-13T17:46:10.630 回答
1

您需要检查发布文件的扩展名或其 MIME 类型,以检测其格式是否正确。

去获取 MIME 类型,检查ContentType属性。

ASP.NET 如何获取 MIME 类型

于 2013-11-13T17:46:59.820 回答
0

如果要查找特定文件类型,可以使用 Path.GetExtension 方法。

string fileExtension = Path.GetExtension(filename);

    if (fileExtension == ".doc") //or whatever file type your looking for
      {
        try
         { do your work }
      }
于 2013-11-13T17:52:51.653 回答