Develop the project using the following method in the NAPA Tool.

On your Developer Site, open the "Napa" Office 365 Development Tools and then choose Add New Project.

Prerequisites

The following are important steps to be done before creating the app.

Specify the permissions that your app needs as in the following.

Choose the Properties button at the bottom of the page.

Default ASPX


  1. <input id="getFile" type="file"/><br />
  2. <input id="displayName" type="text" value="Enter a unique name" /><br />
  3. <input id="addFileButton" type="button" value="Upload" onclick="uploadFile()"/>
Source Code
  1. 'use strict';
  2. var appWebUrl, hostWebUrl;
  3. $(document).ready(function ()
  4. {
  5. // Check for FileReader API (HTML5) support.
  6. if (!window.FileReader) {
  7. alert(' FileReader API does not support Your Current Browser.');
  8. }
  9. // Get the app web and host web URLs.
  10. appWebUrl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
  11. hostWebUrl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
  12. });
  13. // Upload the file.
  14. // You can upload files up to 2 GB with the REST API.
  15. function uploadFile()
  16. {
  17. // Define the folder path for this example.
  18. var serverRelativeUrlToFolder = '/sites/apps/shared documents';
  19. // Get test values from the file input and text input page controls.
  20. // The display name must be unique every time you run the example.
  21. var fileInput = $('#getFile');
  22. var newName = $('#displayName').val();
  23. // Initiate method calls using jQuery promises.
  24. // Get the local file as an array buffer.
  25. var getFile = getFileBuffer();
  26. getFile.done(function (arrayBuffer) {
  27. // Add the file to the SharePoint folder.
  28. var addFile = addFileToFolder(arrayBuffer);
  29. addFile.done(function (file, status, xhr) {
  30. // Get the list item that corresponds to the uploaded file.
  31. var getItem = getListItem(file.d.ListItemAllFields.__deferred.uri);
  32. getItem.done(function (listItem, status, xhr) {
  33. // Change the display name and title of the list item.
  34. var changeItem = updateListItem(listItem.d.__metadata);
  35. changeItem.done(function (data, status, xhr) {
  36. alert('file uploaded successfully in Library);
  37. });
  38. changeItem.fail(onError);
  39. });
  40. getItem.fail(onError);
  41. });
  42. addFile.fail(onError);
  43. });
  44. getFile.fail(onError);
  45. // Get the local file as an array buffer.
  46. function getFileBuffer()
  47. {
  48. var deferred = jQuery.Deferred();
  49. var reader = new FileReader();
  50. reader.onloadend = function (e) {
  51. deferred.resolve(e.target.result);
  52. }
  53. reader.onerror = function (e) {
  54. deferred.reject(e.target.error);
  55. }
  56. reader.readAsArrayBuffer(fileInput[0].files[0]);
  57. return deferred.promise();
  58. }
  59. // Add the file to the file collection in the Shared Documents folder.
  60. function addFileToFolder(arrayBuffer)
  61. {
  62. // Get the file name from the file input control on the page.
  63. var parts = fileInput[0].value.split('\\');
  64. var fileName = parts[parts.length - 1];
  65. // Construct the endpoint.
  66. var fileCollectionEndpoint = String.format(
  67. "{0}/_api/sp.appcontextsite(@target)/web/getfolderbyserverrelativeurl('{1}')/files" +
  68. "/add(overwrite=true, url='{2}')?@target='{3}'",
  69. appWebUrl, serverRelativeUrlToFolder, fileName, hostWebUrl);
  70. // Send the request and return the response.
  71. // This call returns the SharePoint file.
  72. return $.ajax({
  73. url: fileCollectionEndpoint,
  74. type: "POST",
  75. data: arrayBuffer,
  76. processData: false,
  77. headers: {
  78. "accept": "application/json;odata=verbose",
  79. "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
  80. "content-length": arrayBuffer.byteLength
  81. }
  82. });
  83. }
  84. // Get the list item that corresponds to the file by calling the file's ListItemAllFields property.
  85. function getListItem(fileListItemUri)
  86. {
  87. // Construct the endpoint.
  88. // The list item URI uses the host web, but the cross-domain call is sent to the
  89. // app web and specifies the host web as the context site.
  90. fileListItemUri = fileListItemUri.replace(hostWebUrl, '{0}');
  91. fileListItemUri = fileListItemUri.replace('_api/Web', '_api/sp.appcontextsite(@target)/web');
  92. var listItemAllFieldsEndpoint = String.format(fileListItemUri + "?@target='{1}'",
  93. appWebUrl, hostWebUrl);
  94. // Send the request and return the response.
  95. return $.ajax({
  96. url: listItemAllFieldsEndpoint,
  97. type: "GET",
  98. headers: { "accept": "application/json;odata=verbose" }
  99. });
  100. }
  101. // Change the display name and title of the list item.
  102. function updateListItem(itemMetadata)
  103. {
  104. // Construct the endpoint.
  105. // Specify the host web as the context site.
  106. var listItemUri = itemMetadata.uri.replace('_api/Web', '_api/sp.appcontextsite(@target)/web');
  107. var listItemEndpoint = String.format(listItemUri + "?@target='{0}'", hostWebUrl);
  108. // Define the list item changes. Use the FileLeafRef property to change the display name.
  109. // For simplicity, also use the name as the title.
  110. // The example gets the list item type from the item's metadata, but you can also get it from the
  111. // ListItemEntityTypeFullName property of the list.
  112. var body = String.format("{{'__metadata':{{'type':'{0}'}},'FileLeafRef':'{1}','Title':'{2}'}}",
  113. itemMetadata.type, newName, newName);
  114. // Send the request and return the promise.
  115. // This call does not return response content from the server.
  116. return $.ajax({
  117. url: listItemEndpoint,
  118. type: "POST",
  119. data: body,
  120. headers: {
  121. "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
  122. "content-type": "application/json;odata=verbose",
  123. "content-length": body.length,
  124. "IF-MATCH": itemMetadata.etag,
  125. "X-HTTP-Method": "MERGE"
  126. }
  127. });
  128. }
  129. }
  130. // Display error messages.
  131. function onError(error)
  132. {
  133. alert(error.responseText);
  134. }
  135. // Get parameters from the query string.
  136. // For production purposes you may want to use a library to handle the query string.
  137. function getQueryStringParameter(paramToRetrieve)
  138. {
  139. var params = document.URL.split("?")[1].split("&");
  140. for (var i = 0; i < params.length; i = i + 1) {
  141. var singleParam = params[i].split("=");
  142. if (singleParam[0] == paramToRetrieve) return singleParam[1];
  143. }
  144. }
Publish

Publish the App and click the Trust it Button.


Output

File uploaded successfully in Document Library.



Reference: MSDN

I hope you liked this article.