简体   繁体   中英

How To save Excel file to sharepoint using javascript

i am totally new sharepoint please share you code with me

i am having following code only.

<input id="getFile" type="file" onchange="uploadToSharePoint(this)" /><br />

The typical way to work with files in pure JavaScript is by using the FileReader API, which is supported by Internet Explorer 10+ (but not when IE is running in compatibility mode).

Using HTML5 and the Rest API

Assuming you're on at least SharePoint 2013, Microsoft provides documentation for uploading a file using jQuery and the REST API here: Upload a file by using the REST API and jQuery .

This excerpt describes the high-level approach:

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:

  1. 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.

  2. 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('')/rootfolder/files/add ).

  3. Get the list item that corresponds to the uploaded file by using the ListItemAllFields property of the uploaded file.

  4. Change the display name and title of the list item by using a MERGE request.

Abusing the Built-In Upload Page

If you're on SharePoint 2010, you probably can't use HTML5 (SP2010 usually forces Internet Explorer into compatibility mode to get all the Office add-ins to work correctly).

An alternative approach, used by Paul Tavares is to combine AJAX and the built-in default upload page, letting the built-in page to do the work of actually uploading the file.

Here's an excerpt in which Paul explains how it works:

The upload plugin allows a user to upload a file via “ajax” without leaving the page that they are currently on.

...

The plugin is built around the default SharePoint upload page and uses it in the background to actually upload the file. It is also able to accommodate Document Libraries where a 'Check In' is needed, by displaying the default Edit page.

Paul's example uses jQuery, jQuery UI, and SPServices, but you wouldn't necessarily need any of those libraries to achieve the same functionality (though they could certainly save you some time).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM