0

I am trying to check if a file from a multiple upload exists in the path already, if so i want to chuck out a validation error. Its the else if part that is not working. The number directory gets created in the ajax controller server side, but i want to do a check before they upload further files with the same name to that directory. Is this possible? What am i doing wrong here?

function makeProgress(number){   
          var url = getRelativeURL("web/fileUpload");   
          var formData = new FormData();
          formData.append('number', number);
          fls = document.getElementById("attachmentFileUploadInput").files; //number of files... 
          console.log(fls);
          var location = "C:/temp/" + number + "/";
          console.log(location);
          // maximum number of files at a time is 10
          if(fls.length >= 11){
              FileUploadLengthVisible(true);
              return false;
          }
          var j;
          for(j=0;j<fls.length;j++){
              if (fls[j].size > 5000000) //5MB size per file
            {
                  FileUploadSizeVisible(true);

                  return false;
            }
              else if (location + fls[j] == true)
        {
                  alert("file exists");
                  return false;
        }
              else
                  {
              formData.append('files[]', fls[j]);  //note files[] not files
              $.ajax({
                  url : url,
                  data : formData,
                  processData : false,
                  cache: false,
                  contentType: false,
                  type : 'POST',
                  success : function(data) {
                   FileUploadVisible(true);

                   $('#attachmentModal').modal('hide')
                   $(':input','#attachmentModal').val("");
                    $("#pbarmain").hide();
                    $("#pbar").hide();
                    $("#actionPlanDiv").hide();
                    setObjectEnabled('#Upload',false);

                  },
                  error : function(err) {
                      FileUploadErrorVisible(true);

                  }

             });
              console.log('loop each file working');
          }
          }
          console.log("form data " + formData);


            }
trincot
  • 317,000
  • 35
  • 244
  • 286
RA19
  • 709
  • 7
  • 28
  • `location + fls[j]` is trying to concatenate an object and a string. Beyond that it's not really clear what your validation logic is meant to be. Shouldn't you be checking what's on server? – charlietfl Apr 15 '17 at 19:07
  • I am trying to upload a file to a location locally, however if the file exists in this path then I want validation to say file already exists in location. Does this make sense? At the moment, it is just overwriting the file if it exists. – RA19 Apr 15 '17 at 19:11
  • 1
    Not really because you don't have access to local files in browser for security reasons....even if you are running a localhost server – charlietfl Apr 15 '17 at 19:12
  • _"I am trying to check if a file from a multiple upload exists in the path already"_, _" I am trying to upload a file to a location locally, however if the file exists in this path then I want validation to say file already exists in location."_ The file would exist at path at each instance, yes? Also, generally, the full path to file is not available at `path component`. – guest271314 Apr 15 '17 at 19:13
  • @charlietfl If appropriate flags are set at browser local files can be accessed at browser. – guest271314 Apr 15 '17 at 19:14
  • I have a browser button which i can select multiple files, when i select a file it uploads to this location. However when i go in and select the same file, which already exists - i want validation to state "file exists". Sorry not sure if this is possible, thought it might be? – RA19 Apr 15 '17 at 19:15
  • 1
    yes it is possible...but typically you would want to send file name to server and check if file exists using server code. And do upload after that if it doesn't exist – charlietfl Apr 15 '17 at 19:16
  • 1
    You can store `.name` of uploaded `File` object at `sessionStorage` or `localStorage`, then check if the file name exists at `sessionStorage`. See also [How FileReader.readAsText in HTML5 File API works?](http://stackoverflow.com/questions/40146768/how-filereader-readastext-in-html5-file-api-works/40146883?s=1|0.0000#40146883) for description of path components – guest271314 Apr 15 '17 at 19:16
  • Also file names change be changed server side if needed. Best approach would depend on your overall goals which aren't very specific here – charlietfl Apr 15 '17 at 19:19

0 Answers0