Uploading Only Text Files using FileUpload Control using Required Validation

Introduction

Mentioned code is for uploading only text files using file upload control, many a times even a file which is not of extension .txt is uploaded in the database. Used a validation in the below code which will help the developers to allow only text files to be uploaded without special characters.

protected void UploadButton_Click(object sender, EventArgs e)
{
    if (FileUploadControl.PostedFile != null && FileUploadControl.PostedFile.FileName != "" && FileUploadControl.PostedFile.ContentLength >
0)
    {
        try
        {
           System.IO.BinaryReader r = new System.IO.BinaryReader(FileUploadControl.PostedFile.InputStream);
           string fileclass = "";
           byte buffer;
           try
           {
               buffer = r.ReadByte();
               fileclass = buffer.ToString();
               buffer = r.ReadByte();
               fileclass += buffer.ToString();
            }
            catch (Exception ex)
            {
               Message.ClearMessage();
               Message.ShowMessage(ex.Message.ToString(), AppMessages.MessageType.Error);
               return;
            }
            if (!(fileclass == "8269" || fileclass == "4948"))
            {
               Message.ClearMessage();
               Message.ShowMessage("The upload file must be in plain text format.", AppMessages.MessageType.Error);
               r.Close();
               return;
           }
           FileUploaded fileUpload = new FileUploaded();
           filename = Path.GetFileName(FileUploadControl.FileName);
           string FileUploadLocation = Server.MapPath("~/") + "UserData//" + filename;
           FileUploadControl.SaveAs(FileUploadLocation);
           r.Close();
           using (FileStream fs = new FileStream(FileUploadLocation, FileMode.Open, FileAccess.Read))
           {
              byte[] imageData = new Byte[fs.Length];
              fs.Read(imageData, 0, System.Convert.ToInt32(fs.Length));
              fs.Close();
              fileUpload.Fileobject = imageData;
           }
           string text = string.Empty;
           using (System.IO.StreamReader file = new System.IO.StreamReader(FileUploadLocation))
           {A
             text = file.ReadToEnd();
           }
           Regex objAlphaPattern = new Regex(@"^[a-zA-Z0-9|-]*$");
           text = text.Replace("\r\n", "").Replace("\n", "").Replace("\r", "");
           if (!(objAlphaPattern.IsMatch(text)))
           {
              File.Delete(FileUploadLocation);
              Message.ClearMessage();
              Message.ShowMessage("The upload file contains special characters.", AppMessages.MessageType.Error);
              return;
           }
           File.Delete(FileUploadLocation);
           fileUpload.Filename = filename;
       }
       catch (Exception ex)
       {
           Message.ClearMessage();
           Message.ShowMessage(ex.Message.ToString(), AppMessages.MessageType.Error);
       }
    }
}

Ebook Download
View all
Learn
View all