1

I have the following piece of code which uploads a file and checks its validity. First issue:

if (RadUpload1.UploadedFiles.Count == 0)
{
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
        ErrorsList.ERR_P_DATE_FILE_VALID);
}
else
{
    if (RadUpload1.UploadedFiles.Count > 0)
    {
        foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
        {
            FileInfo fi = new FileInfo(validFile.FileName);
            Stream fs = validFile.InputStream;

            IDbContextualRecord pFile = statusContext.CreateAndAddRecordForInsert(PartStoredFile.t_name);
            pFile[PartStoredFile.c_partId] = _part[Part.c_id];
            string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] +
                                  "\\partRec\\" + _part[Part.c_id] + "\\" + pFile[PartStoredFile.c_id];

            long bytesOnTheStream = 0L;
            try
            {
                DirectoryInfo dir = new DirectoryInfo(targetFolder);
                if (dir.Exists == false)
                    dir.Create();

                string fullFileName = Path.Combine(targetFolder, fi.Name);

                Stream targetStream = File.OpenWrite(fullFileName);
                byte[] buffer = new Byte[AppConstants.BUFF_SIZE];
                int bytesRead;

                // while the read method returns bytes
                // keep writing them to the output stream
                while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0)
                {
                    targetStream.Write(buffer, 0, bytesRead);
                    bytesOnTheStream += bytesRead;
                }

                fs.Close();
                targetStream.Close();
            }
            catch (Exception ex)
            {
                throw ex;
            }

What I want to do is to check if the number of characters in the filepath name exceeds 260 to display me a message of error.

This is the second issue after the modification was made:

if (RadUpload1.UploadedFiles.Count <= 0)
{
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
                                  ErrorsList.ERR_P_DATE_FILE_VALID);
}
else
{
    if (RadUpload1.UploadedFiles.Count > 0 )
    {
        foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
        {
            pomDoc = (IDbContextualRecord)Session[AppConstants.POM_DOCUMENT_NEW];

            FileInfo fi = new FileInfo(validFile.FileName);
            Stream fs = validFile.InputStream;

            IDbContextualRecord pomFile = pomContext.CreateAndAddRecordForInsert(PomFile.t_name);
            pomFile[PomFile.c_pomDocumentId] = pomDoc[PomDocument.c_id];
            string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] + "\\POM\\" + pomDoc[PomDocument.c_id] + "\\" + pomFile[PomFile.c_id];

            long bytesOnTheStream = 0L;
            try
            {
                DirectoryInfo dir = new DirectoryInfo(targetFolder);
                if (dir.Exists == false)
                    dir.Create();

                string fullFileName = Path.Combine(targetFolder, fi.Name);

                if (fullFileName.Length > 260)
                {
                    throw new Exception(string.Format("The filename is too long!",fullFileName));
                }
                Stream targetStream = File.OpenWrite(fullFileName);
                byte[] buffer = new Byte[AppConstants.BUFF_SIZE];
                int bytesRead;

                // while the read method returns bytes
                // keep writing them to the output stream
                while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0)
                {
                    targetStream.Write(buffer, 0, bytesRead);
                    bytesOnTheStream += bytesRead;
                }

                fs.Close();
                targetStream.Close();
            }
            catch (Exception ex)
            {
                throw ;
            }
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
Nomonom
  • 137
  • 2
  • 11

1 Answers1

1

You just have to compare fullFileName.Lenght to 260 and raise an exception if needed:

if (RadUpload1.UploadedFiles.Count <= 0) // Changed the condition to remove the check within the else block
{
    Session[AppConstants.ERROR_MESSAGE] = ErrorsList.GetErrorMessage(
        ErrorsList.ERR_P_DATE_FILE_VALID);
}
else
{
    foreach (UploadedFile validFile in RadUpload1.UploadedFiles)
    {
        FileInfo fi = new FileInfo(validFile.FileName);
        Stream fs = validFile.InputStream;

        IDbContextualRecord pFile = statusContext.CreateAndAddRecordForInsert(PartStoredFile.t_name);
        pFile[PartStoredFile.c_partId] = _part[Part.c_id];
        string targetFolder = AppSession.Current.ConfigParameters[AppConstants.UPLOAD_FILE_PATH] +
                              "\\partRec\\" + _part[Part.c_id] + "\\" + pFile[PartStoredFile.c_id];

        long bytesOnTheStream = 0L;
        try
        {
            DirectoryInfo dir = new DirectoryInfo(targetFolder);
            if (dir.Exists == false)
                dir.Create();

            string fullFileName = Path.Combine(targetFolder, fi.Name);

            if(fullFileName.Length > 260)
            {
                throw new Exception(string.Format("The filename {0} is too long.", fullFileName));
                // Or do whatever you want
            }

            Stream targetStream = File.OpenWrite(fullFileName);
            byte[] buffer = new Byte[AppConstants.BUFF_SIZE];
            int bytesRead;

            // while the read method returns bytes
            // keep writing them to the output stream
            while ((bytesRead = fs.Read(buffer, 0, AppConstants.BUFF_SIZE)) > 0)
            {
                targetStream.Write(buffer, 0, bytesRead);
                bytesOnTheStream += bytesRead;
            }

            fs.Close();
            targetStream.Close();
        }
        catch (Exception ex)
        {
            throw;
        }

Also, you don't want to throw ex; but rather throw; or this will reset the stacktrace, see Is there a difference between "throw" and "throw ex"?

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • is it necessary to throw an exception : throw new Exception(string.Format("The filename {0} is too long.", fullFileName)); or it can display only a message? – Nomonom May 25 '16 at 07:54
  • Well, I wrote, just under the exception *`// Or do whatever you want`*... @Nomonom – Thomas Ayoub May 25 '16 at 07:59
  • I ask because, I have to files with the same problem. In one the exception is displayed as a message and in the other one is giving me the exception in code. – Nomonom May 25 '16 at 08:02
  • @Nomonom I'm sorry I don't quite understand your last comment, may you rephrase it? – Thomas Ayoub May 25 '16 at 08:04
  • I have 2 pages in the application that must have this limit. One page: Status(the one above) shows me the exception as a message in the page,which is ok. And the PDoc page gives me directly the exception in code as a warning. – Nomonom May 25 '16 at 08:16
  • @Nomonom I start to understand your issue. Please, edit your question to include more context and code so I can help you better. – Thomas Ayoub May 25 '16 at 08:19
  • Edited. @ThomasAyoub – Nomonom May 25 '16 at 08:28
  • Nice @Nomonom. You were talking about a message, could you add it ? – Thomas Ayoub May 25 '16 at 09:04
  • The message won't change from "The filename is too long!". The problem is that I don't understant why on a page take it as a message,while on the other one takes it as a warning. So I assume it should be changed both as message? – Nomonom May 25 '16 at 09:17