SharePoint / sp-dev-docs

SharePoint & Viva Connections Developer Documentation
https://docs.microsoft.com/en-us/sharepoint/dev/
Creative Commons Attribution 4.0 International
1.25k stars 1.01k forks source link

"update a file by using the PUT method" provides zero explanation of encoding for the file #5364

Closed bitsofinfo closed 2 years ago

bitsofinfo commented 4 years ago

The section "update a file by using the PUT method" provides zero explanation of encoding for the file:

POST https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_name})/$value
Authorization: "Bearer " + accessToken
Content-Length: {length of request body as integer}
X-HTTP-Method: "PUT"
X-RequestDigest: "{form_digest_value}"

"Contents of file"

Huh? is this url encoded data? what is it? provide real samples...


Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

msft-github-bot commented 4 years ago

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

OS-Lee commented 4 years ago

Hi, Sample code for your reference, the script execute in SharePoint page(script editor webpart), if you use oauth authentication, send the Bearer token in your request header also.

`

<script type="text/javascript">
    'use strict';

    jQuery(document).ready(function () {

        // Check for FileReader API (HTML5) support.
        if (!window.FileReader) {
            alert('This browser does not support the FileReader API.');
        }
    });

    function UpdateFile() {
        // Define the folder path for this example, the file need exists in the folder
        var serverRelativeUrlToFolder = '/sites/lee/MyDoc/SubFolder';
        // Get test values from the file input and text input page controls.
        var fileInput = jQuery('#getFile');            
        // Get the server URL.
        var serverUrl = _spPageContextInfo.webAbsoluteUrl;
        // Initiate method calls using jQuery promises.
        // Get the local file as an array buffer.
        var getFile = getFileBuffer();
        getFile.done(function (arrayBuffer) {
            // Get the file name from the file input control on the page.
            var parts = fileInput[0].value.split('\\');
            var fileName = parts[parts.length - 1];

            // Construct the endpoint.
            var fileEndpoint = String.format(
                "{0}/_api/web/GetFileByServerRelativeUrl('{1}/{2}')/$value",
                serverUrl, serverRelativeUrlToFolder, fileName);

            // Send the request and return the response.
            // This call update the SharePoint file.
            jQuery.ajax({
                url: fileEndpoint,
                type: "POST",
                data: arrayBuffer,
                processData: false,
                headers: {
                    "accept": "application/json;odata=verbose",
                    "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),                        
                    "X-HTTP-Method": "PUT"
                },
                success: function (result) {
                    console.log("update done");
                },
                error: function (error) {
                    console.log(JSON.stringify(error));
                }
            });
        });
        getFile.fail(onError);

        // Get the local file as an array buffer.
        function getFileBuffer() {
            var deferred = jQuery.Deferred();
            var reader = new FileReader();
            reader.onloadend = function (e) {
                deferred.resolve(e.target.result);
            }
            reader.onerror = function (e) {
                deferred.reject(e.target.error);
            }
            reader.readAsArrayBuffer(fileInput[0].files[0]);
            return deferred.promise();
        }
    }

    // Display error messages.
    function onError(error) {
        alert(error.responseText);
    }
</script>`

Lee Microsoft SharePoint Community Support

ghost commented 2 years ago

This issue is being closed as part of an issue list cleanup project. Issues with no activity in the past 6 months that aren't tracked by engineering as bugs were closed as part of this inititive. If this is still an issue, please follow the steps outlined to re-open or submit a new issue.

ghost commented 2 years ago

Issues that have been closed & had no follow-up activity for at least 7 days are automatically locked. Please refer to our wiki for more details, including how to remediate this action if you feel this was done prematurely or in error: Issue List: Our approach to locked issues