Guest User

Untitled

a guest
Jan 17th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.18 KB | None | 0 0
  1. <input name="getFile" id="getFile" type="file"/>
  2. <input id="addFileButton" onclick="uploadFile()" type="button" value="Upload"/> <script src="/js_css/jquery-1.10.2.js" type="text/javascript"></script>
  3. <script type="text/javascript">
  4. 'use strict';
  5. var fileName;
  6. jQuery(document).ready(function () {
  7.  
  8. // Check for FileReader API (HTML5) support.
  9. if (!window.FileReader) {
  10. alert('This browser does not support the FileReader API.');
  11. }
  12. });
  13.  
  14. // Upload the files
  15. function uploadFile() {
  16.  
  17. // Get test values from the file input
  18. var fileInput = jQuery('#getFile');
  19. // Get the server URL.
  20. var serverUrl = _spPageContextInfo.webAbsoluteUrl;
  21.  
  22. // Get the local file as an array
  23. var getFile = getFileBuffer();
  24. getFile.done(function (arrayBuffer) {
  25.  
  26. // Add the file to the SharePoint Library.
  27. var addFile = addFileToFolder(arrayBuffer);
  28. addFile.done(function (file, status, xhr) {
  29. var getItem = getListItem(file.d.ListItemAllFields.__deferred.uri);
  30. getItem.done(function (listItem, status, xhr) {
  31.  
  32. // Change the display name and title of the list item.
  33. var changeItem = updateListItem(listItem.d.__metadata);
  34. changeItem.done(function (data, status, xhr) {
  35. alert('file uploaded');
  36. $("#getFile").val("");
  37.  
  38. });
  39. changeItem.fail(onError);
  40. });
  41. getItem.fail(onError);
  42. });
  43. addFile.fail(onError);
  44. });
  45. getFile.fail(onError);
  46.  
  47. // Get the local file as an array buffer.
  48. function getFileBuffer() {
  49. var deferred = jQuery.Deferred();
  50. var reader = new FileReader();
  51. reader.onloadend = function (e) {
  52. deferred.resolve(e.target.result);
  53. }
  54. reader.onerror = function (e) {
  55. deferred.reject(e.target.error);
  56. }
  57. reader.readAsArrayBuffer(fileInput[0].files[0]);
  58. return deferred.promise();
  59. }
  60.  
  61. // Add the file to the file collection in the Shared Documents folder.
  62. function addFileToFolder(arrayBuffer) {
  63.  
  64. // Get the file name from the file input control on the page.
  65. var parts = fileInput[0].value.split('\');
  66. fileName = parts[parts.length - 1];
  67.  
  68. // Construct the endpoint.
  69. var fileCollectionEndpoint = String.format(
  70. "{0}/_api/Web/Lists/getByTitle('TestDL')/RootFolder/Files/Add(url='{1}', overwrite=true)",
  71. serverUrl, fileName);
  72.  
  73. // Send the request and return the response.
  74. // This call returns the SharePoint file.
  75. return jQuery.ajax({
  76. url: fileCollectionEndpoint,
  77. type: "POST",
  78. data: arrayBuffer,
  79. processData: false,
  80. headers: {
  81. "accept": "application/json;odata=verbose",
  82. "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
  83. "content-length": arrayBuffer.byteLength
  84. }
  85. });
  86. }
  87.  
  88. // Get the ListItemAllFields property.
  89. function getListItem(fileListItemUri) {
  90.  
  91. // Send the request and return the response.
  92. return jQuery.ajax({
  93. url: fileListItemUri,
  94. type: "GET",
  95. headers: { "accept": "application/json;odata=verbose" }
  96. });
  97. }
  98.  
  99. // Change the display name and title of the list item if you want.
  100. function updateListItem(itemMetadata) {
  101. var body = String.format("{{'__metadata':{{'type':'{0}'}},'FileLeafRef':'{1}','Title':'{2}'}}",
  102. itemMetadata.type, fileName, fileName);
  103.  
  104. // Send the request and return the promise.
  105. // This call not return response content from the server.
  106. return jQuery.ajax({
  107. url: itemMetadata.uri,
  108. type: "POST",
  109. data: body,
  110. headers: {
  111. "X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
  112. "content-type": "application/json;odata=verbose",
  113. "content-length": body.length,
  114. "IF-MATCH": itemMetadata.etag,
  115. "X-HTTP-Method": "MERGE"
  116. }
  117. });
  118. }
  119. }
  120.  
  121. // Display error messages.
  122. function onError(error) {
  123. alert(error.responseText);
  124. }
  125. </script>
Add Comment
Please, Sign In to add comment