Search This Blog

Thursday, September 11, 2014

Upload only PDF and XSL files for SharePoint Lists

function OkAttach() {
    a: ;
    var g = FileuploadString + String(FileUploadIndex), b = GetAttachElement(g), f = TrimWhiteSpaces(b.value);
    if (!Boolean(f)) {
        alert(Strings.STS.L_FileNameRequired_TXT);
        b.focus()
    }
     else if(CustomizedCheck(b.value)==false) {
           if(_spPageContextInfo.serverRequestPath.indexOf("Listname1") > -1)
           {
            alert("Invalid File Type. (Only PDF/PPTX files are allowed.)");
           }
           else if(_spPageContextInfo.serverRequestPath.indexOf("ListName2") > -1)
           {
            alert("Invalid File Type. (Only PDF/XLS files are allowed.)");
           }
           else
           {
            alert("Invalid File Type. (Only PDF files are allowed.)");
           }
            b.value = "";
            b.focus();
        }
    else if(CheckFileExist(f)==false){
        alert("File already exists. Please select a different file.");
    }
    else {
        var c = document.getElementById("idAttachmentsTable").insertRow(-1), h = "attachRow" + String(FileUploadIndex);
        c.id = h;
        var d = c.insertCell(-1);
        d.className = "ms-vb";
        d.innerHTML = '<span dir="ltr">' + f + "</span>&nbsp;&nbsp;&nbsp;&nbsp;";
        var e = c.insertCell(-1); e.className = "ms-propertysheet";
        var k = document.getElementsByName("RectGifUrl")[0];
        e.innerHTML = '<span class="ms-delAttachments"><IMG SRC=\'' + k.value + "'>&nbsp;<a href='javascript:RemoveLocal(\"" + h + '","' + g + "\")'>" + Strings.STS.L_Delete_Text + "</a></span>";
        b.style.display = "none";
        ++FileUploadIndex;
        ++FileUploadLocalFileCount;
        var i = document.getElementById("attachmentsOnClient"), a = document.createElement("input");
        a.tabIndex = 1; a.type = "File";
        a.className = "ms-longfileinput";
        a.title = Strings.STS.L_FileUploadToolTip_text;
        a.name = FileuploadString + String(FileUploadIndex);
        a.id = FileuploadString + String(FileUploadIndex);
        a.size = 56; i.appendChild(a);
        var j = b.form;
        j.encoding = "multipart/form-data";
        document.getElementById("idAttachmentsRow").style.display = "";
        ShowPart1()       

    }
}

function CustomizedCheck(file) {
    var ext = file.match(/\.([^\.]+)$/)[1];
    switch(ext)
    {
        case "pdf":
        {                  
             return true;
        }
        case "pptx":
        {
         if(_spPageContextInfo.serverRequestPath.indexOf("Listname1") > -1) // allow PPTX upload only for Account Reviews.
         {
             return true;
         }
         else
         {
          return false;
         }
        }
        break;
        case "xls":
        case "xlsx":
        {
         if(_spPageContextInfo.serverRequestPath.indexOf("Listname2") > -1) // allow PPTX upload only for Account Reviews.
         {
             return true;
         }
         else if(_spPageContextInfo.serverRequestPath.indexOf("Listname3") > -1)
         {
             return true;
         }
         else if(_spPageContextInfo.serverRequestPath.indexOf("Listname4") > -1)
         {
             return true;
         }
         else
         {
          return false;
         }
        }
        break;
        default:
        {               
            //document.getElementById('onetidIOFile').value="";
              return false;
        }
    }
}

function CheckFileExist(file){
    var TempCompareText;
    //var Tempfile = file.split('.')[0];
    var Tempfile = file;
    if(Tempfile.length > 0)
    {
        Tempfile = Tempfile.replace('C:\\fakepath\\','');
    }
    var Rowcount = document.getElementById("idAttachmentsTable").rows.length;
    for(var i=0; i <Rowcount; i++)
    {
        //var TextValue = document.getElementById("idAttachmentsTable").rows[i].innerText; //Commented - 26-12-2013
        // Firefox uses the W3C-compliant textContent property. So changing from innerText - 12.26.2013
        var TextValue = document.getElementById("idAttachmentsTable").rows[i].textContent;
        if(!TextValue)
            TextValue = document.getElementById("idAttachmentsTable").rows[i].innerText; // IE 8 does not support textContent property
       
        //TempCompareText = TextValue.split('.')[0];
        TempCompareText = TextValue.replace("Delete",'').trim()
        if(TempCompareText.length > 0)
        {
            TempCompareText = TempCompareText.replace('C:\\fakepath\\','');
        }
        if(Tempfile.toUpperCase().trim() === TempCompareText.toUpperCase().trim())
        {
            return false;   
        }
    }
}

No comments:

Post a Comment