仅上传文档文件

时间:2013-11-13 17:42:46

标签: c# asp.net file-upload

我正在尝试上传文件.user可以上传theri文件,但他/她可以上传图片而不是文件,我想限制这个如何申请条件这是我的上传代码

            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 个答案:

答案 0 :(得分: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";
    }
}

答案 1 :(得分:1)

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

if (string.Equals(fileExt, ".pdf", StringComparison.OrdinalIgnoreCase)) {...}

答案 2 :(得分:1)

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

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

ASP.NET How to get MIME Type

答案 3 :(得分:0)

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

string fileExtension = Path.GetExtension(filename);

    if (fileExtension == ".doc") //or whatever file type your looking for
      {
        try
         { do your work }
      }