1

How to upload file in sharepoint 2010 using javascript?

Please give me solution!

Thanks in advance!

1
1

Insert below code in a content editor web part. Ref that's it, you are done!

** below code will upload the file in site default document library, you can change the path in case you want.

<html>  
<head>  

    <script language="javascript" type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js"></script>  
    <script language="javascript" type="text/javascript">  
        varfileInput;  
        $(document).ready(function()  
        {  
            fileInput = $("#getFile");  
            SP.SOD.executeFunc('sp.js', 'SP.ClientContext', registerClick);  
        });  

        function registerClick()  
        {  
            //Register File Upload Click Event  
            $("#addFileButton").click(readFile);  
        }  
        vararrayBuffer;  

        function readFile()  
        {  
            //Get File Input Control and read th file name  
            varelement = document.getElementById("getFile");  
            varfile = element.files[0];  
            varparts = element.value.split("\\");  
            varfileName = parts[parts.length - 1];  
            //Read File contents using file reader  
            varreader = new FileReader();  
            reader.onload = function(e)  
            {  
                uploadFile(e.target.result, fileName);  
            }  
            reader.onerror = function(e)  
            {  
                alert(e.target.error);  
            }  
            reader.readAsArrayBuffer(file);  
        }  
        varattachmentFiles;  

        function uploadFile(arrayBuffer, fileName)  
        {  
            //Get Client Context,Web and List object.  
            varclientContext = new SP.ClientContext();  
            varoWeb = clientContext.get_web();  
            varoList = oWeb.get_lists().getByTitle('Documents');  
            //Convert the file contents into base64 data  
            varbytes = new Uint8Array(arrayBuffer);  
            vari, length, out = '';  
            for (i = 0, length = bytes.length; i < length; i += 1)  
            {  
                out += String.fromCharCode(bytes[i]);  
            }  
            varbase64 = btoa(out);  
            //Create FileCreationInformation object using the read file data  
            varcreateInfo = new SP.FileCreationInformation();  
            createInfo.set_content(base64);  
            createInfo.set_url(fileName);  
            //Add the file to the library  
            varuploadedDocument = oList.get_rootFolder().get_files().add(createInfo)  
            //Load client context and execcute the batch  
            clientContext.load(uploadedDocument);  
            clientContext.executeQueryAsync(QuerySuccess, QueryFailure);  
        }  

        function QuerySuccess()  
        {  
            console.log('File Uploaded Successfully.');  
        }  

        function QueryFailure(sender, args)  
        {  
            console.log('Request failed with error message - ' + args.get_message() + ' . Stack Trace - ' + args.get_stackTrace());  
        }  
    </script>  
</head>  

<body>  
    <input id="getFile" type="file" /><br />  
    <input id="addFileButton" type="button" value="Upload" />  
</body>  
<html>  

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.