Nis
Nis

Reputation: 27

401 error while uploading file to Sharepoint 2013 using REST API

I have a simple html form where i have request is built to upload a file to Sharepoint Server 2013 using REST ( taken reference of the Code from the net ) .

Following is the code snippet

<html>
<head>
<script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.9.1.min.js" type="text/javascript"></script>
<script>
function AddAttachments()
{
var digest = "";
$.ajax(
{
                url: "http://vmjnjlabeling01:22216/as/Shared Documents/_api/contextinfo",
                method: "POST",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose",
                                "Authorization" : "Basic c3AyMDEzOlNoYXJlcG9pbnRAMjAxMw=="
                },
                success: function (data) {
                digest = data.d.GetContextWebInformation.FormDigestValue;
                },
                error: function (data) {

                }
}).done(function() {
                var fileInput = $('#uploadFile');
                var fileName = fileInput[0].files[0].name;
                var reader = new FileReader();
                reader.onload = function (e) {
                var fileData = e.target.result;
                                var res11 = $.ajax(
                                {                             
                                                //url: "http://vmjnjlabeling01:22216/as/_api/web/lists/getbytitle('DocTest')/items(1)/AttachmentFiles/ add(FileName='" + fileName + "')",  
                                                url:"http://vmjnjlabeling01:22216/as/_api/web/getfolderbyserverrelativeurl('/as/Shared Documents')/Files/add(overwrite=true, url='" + fileName +  "')",
                                                method: "POST",
                                                binaryStringRequestBody: true,
                                                data: fileData,
                                                processData: false,
                                                headers: {
                                                                "ACCEPT": "application/json;odata=verbose",                                                                                                                                   
                                                                "X-RequestDigest": digest,
                                                               // "content-length": fileData.byteLength
                                                },                                                                                                                            
                                                success: function (data) {                                            

                                                },
                                                error: function (data) {
                                                                alert("Error occured." + data.responseText);
                                                }
                                });                          
                };
                reader.readAsArrayBuffer(fileInput[0].files[0]);

});                                          
}
</script>
</head>
<body>
<div>
                <input id="uploadFile" type="file">
</div>

<div>
                <input type="submit" onclick="AddAttachments()" value="Add Attachments"> </input>
</div>
</body>
</html>

But when i upload the document i am getting the 401 Error everytime despite all permissions have been given .

Please find the screenshot : enter image description hereenter image description here

Any help on this topic to resolve this issue is most appreciated.

Regards, N

Upvotes: 0

Views: 1374

Answers (2)

Vaseem AbdulSamad
Vaseem AbdulSamad

Reputation: 54

You need to enable Basic Authentication credentials are sent in clear text in Authentication Providers. Goto Central Admin>> Security>>Security Authentication providers>> In Right side dropdown select the Application and click on default

Tick Basic Authentication in Claims Authentication Types

Upvotes: 1

Marc D Anderson
Marc D Anderson

Reputation: 479

Because your page isn't inside SharePoint (you're not getting any "SharePoint" in it), you'll need to authorize differently than if it were a SharePoint page. The easiest answer is probably to look at how the Add-in model works. On the other hand, since there's nothing going on in the page other than your attempt to upload an attachment, could the page be a SharePoint page?

Upvotes: 0

Related Questions