Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. <div id="file">
  2. <apex:inputFile value="{!attachment.body}" filename="{!attachment.name}" id="file" title="Upload Attachment" styleclass="fileType"/>
  3. </div>
  4.  
  5. <label>Select file: <input type="file" name="imagefile" accept="image/jpeg, image/png">
  6. </label>
  7. <input type="submit" value="upload">
  8.  
  9. 'value="{!attachment.body}" filename="{!attachment.name}"'
  10.  
  11. <input id="file-input" type="file" name="file"/>
  12. <input type="button" value="Upload" onclick="uploadFile();"/>
  13.  
  14. <script type="text/javascript">
  15. var __sfdcSessionId = '{!GETSESSIONID()}';
  16. </script>
  17. <script src="/soap/ajax/29.0/connection.js" type="text/javascript"></script>
  18.  
  19. function uploadFile()
  20. {
  21. var input = document.getElementById('file-input');
  22. var parentId = // Your ID here, I suggest using a merge field to get this
  23.  
  24. var filesToUpload = input.files;
  25.  
  26. for(var i = 0, f; f = filesToUpload[i]; i++)
  27. {
  28. var reader = new FileReader();
  29.  
  30. // Keep a reference to the File in the FileReader so it can be accessed in callbacks
  31. reader.file = f;
  32.  
  33. reader.onerror = function(e)
  34. {
  35. switch(e.target.error.code)
  36. {
  37. case e.target.error.NOT_FOUND_ERR:
  38. alert('File Not Found!');
  39. break;
  40. case e.target.error.NOT_READABLE_ERR:
  41. alert('File is not readable');
  42. break;
  43. case e.target.error.ABORT_ERR:
  44. break; // noop
  45. default:
  46. alert('An error occurred reading this file.');
  47. };
  48. };
  49.  
  50. reader.onabort = function(e)
  51. {
  52. alert('File read cancelled');
  53. };
  54.  
  55. reader.onload = function(e)
  56. {
  57. var att = new sforce.SObject("Attachment");
  58. att.Name = this.file.name;
  59. att.ContentType = this.file.type;
  60. att.ParentId = parentId;
  61.  
  62. att.Body = (new sforce.Base64Binary(e.target.result)).toString();
  63.  
  64. sforce.connection.create([att],
  65. {
  66. onSuccess : function(result, source)
  67. {
  68. if (result[0].getBoolean("success"))
  69. {
  70. console.log("new attachment created with id " + result[0].id);
  71. }
  72. else
  73. {
  74. console.log("failed to create attachment " + result[0]);
  75. }
  76. },
  77. onFailure : function(error, source)
  78. {
  79. console.log("An error has occurred " + error);
  80. }
  81. });
  82. };
  83.  
  84. reader.readAsBinaryString(f);
  85. }
  86. }
  87.  
  88. <label>Select file: <input type="file" name="imagefile" accept="image/jpeg, image/png">
  89. </label>
  90. <input type="submit" value="upload">
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement