![](/uploads/1/2/5/5/125537968/949431409.jpeg)
-->
The code examples in this article use the REST interface and jQuery AJAX requests to add a local file to the Documents library, and then change properties of the list item that represents the uploaded file.
This process uses the following high-level steps:
Human Body: Pushing the Limits takes you across continents and introduces you to people who have pushed their bodies to the max. This groundbreaking documentary uses CGI technology and hi-tech camera work to examine their physical ordeals in vivid detail both externally and internally! This episode explains the human brain, the most powerful organ in the body. Viewers will learn about the brain as a central processing unit that draws as many electrical impulses in one day as all of the telephones around the world.
- Convert the local file to an array buffer by using the FileReader API, which requires HTML5 support. The jQuery(document).ready function checks for FileReader API support in the browser.
- Add the file to the Shared Documents folder by using the Add method on the folder's file collection. The array buffer is passed in the body of the POST request.These examples use the getfolderbyserverrelativeurl endpoint to reach the file collection, but you can also use a list endpoint (example:
…/_api/web/lists/getbytitle('<list title>')/rootfolder/files/add
). - Get the list item that corresponds to the uploaded file by using the ListItemAllFields property of the uploaded file.
- Change the display name and title of the list item by using a MERGE request.
Running the code examples
Both code examples in this article use the REST API and jQuery AJAX requests to upload a file to the Shared Documents folder and then change list item properties.
The first example uses SP.AppContextSite to make calls across SharePoint domains, like a SharePoint-hosted add-in would do when uploading files to the host web.
The second example makes same-domain calls, like a SharePoint-hosted add-in would do when uploading files to the add-in web, or a solution that's running on the server would do when uploading files.
Note
![Poison princess Poison princess](http://bonlacfoods.com/images/human-body-pushing-the-limits-brain-power-worksheet/human-body-pushing-the-limits-brain-power-worksheet-16.jpg)
Provider-hosted add-ins written in JavaScript must use the SP.RequestExecutor cross-domain library to send requests to a SharePoint domain. For an example, see upload a file by using the cross-domain library.
To use the examples in this article, you'll need the following:
- SharePoint Server or SharePoint Online.
- Write permissions to the Documents library for the user running the code. If you're developing a SharePoint Add-in, you can specify Write add-in permissions at the List scope.
- Browser support for the FileReader API (HTML5).
- A reference to the jQuery library in your page markup. For example:
- The following controls in your page markup.
Code example 1: Upload a file across SharePoint domains by using the REST API and jQuery
The following code example uses the SharePoint REST API and jQuery AJAX requests to upload a file to the Documents library and to change properties of the list item that represents the file. The context for this example is a SharePoint-hosted add-in that uploads a file to a folder on the host web.
You need to meet these requirements to use this example.
Code example 2: Upload a file in the same domain by using the REST API and jQuery
The following code example uses the SharePoint REST API and jQuery AJAX requests to upload a file to the Documents library and to change properties of the list item that represents the file. The context for this example is a solution that's running on the server. The code would be similar in a SharePoint-hosted add-in that uploads files to the add-in web.
You need to meet these requirements before you can run this example.
See also
![](/uploads/1/2/5/5/125537968/949431409.jpeg)