|  |  | 
 |  |  | <!-- 该页面用于Lead对象上传PDF,未来如果要添加其他对象的上传PDF功能,复制该页面,将**standardController**修改为其他对象API名称即可 --> | 
 |  |  | <apex:page standardController="Lead" extensions="FileUploadController" id="page" lightningStyleSheets="true"> | 
 |  |  |     <apex:includeScript value="{! URLFOR($Resource.AWSService, 'AWSService.js') }" /> | 
 |  |  |     <apex:includeScript value="{!URLFOR($Resource.jquery183minjs)}"/> | 
 |  |  |     <script> | 
 |  |  |         var staticResources = JSON.parse('{!staticResource}'); | 
 |  |  |         var parentId = '{!parentId}'; | 
 |  |  | 
 |  |  |             var fileObject = document.getElementById("page:theForm:block:uploadSection:file"); | 
 |  |  |             var reader = new FileReader(); | 
 |  |  |             var data = reader.readAsDataURL(fileObject.files[0]); | 
 |  |  |             debugger | 
 |  |  |             console.log(event); | 
 |  |  |         } | 
 |  |  |         function getBase64(file) { | 
 |  |  |             return new Promise((resolve, reject) => { | 
 |  |  |                 const reader = new FileReader(); | 
 |  |  |                 reader.readAsDataURL(file); | 
 |  |  |                 reader.onload = () => resolve(reader.result); | 
 |  |  |                 reader.onerror = error => reject(error); | 
 |  |  |                 reader.onload = function(){resolve(reader.result)} ; | 
 |  |  |                 reader.onerror =function(error){reject(error)}; | 
 |  |  |             }); | 
 |  |  |         } | 
 |  |  |         function disableButtonStatus() { | 
 |  |  | 
 |  |  |             disableButtonStatus(); | 
 |  |  |             var fileObject = document.getElementById("file").files[0]; | 
 |  |  |             getBase64(fileObject).then( | 
 |  |  |                 data => { | 
 |  |  |                function(data) { | 
 |  |  |                     console.log(data); | 
 |  |  |                     uploadFileToAWS(data, (fileObject.size).toString(), fileObject.name); | 
 |  |  |                      | 
 |  |  | 
 |  |  |         } | 
 |  |  |         function confirmTrans(transId, isSuccess) { | 
 |  |  |              | 
 |  |  |             fetch(staticResources.updateUrl, { | 
 |  |  |                 method: 'POST', | 
 |  |  |                 body: JSON.stringify({ 'txId': transId, "isSuccess": isSuccess }), | 
 |  |  |                 headers: { | 
 |  |  |                     'Content-Type': 'application/json', | 
 |  |  |                     'pi-token': staticResources.token | 
 |  |  |                 } | 
 |  |  |             }).then((data) => { | 
 |  |  |                 return data.json(); | 
 |  |  |             }).then(data => { | 
 |  |  |             // fetch(staticResources.updateUrl, { | 
 |  |  |             //     method: 'POST', | 
 |  |  |             //     body: JSON.stringify({ 'txId': transId, "isSuccess": isSuccess }), | 
 |  |  |             //     headers: { | 
 |  |  |             //         'Content-Type': 'application/json', | 
 |  |  |             //         'pi-token': staticResources.token | 
 |  |  |             //     } | 
 |  |  |             // }).then(function(data) { | 
 |  |  |             //     return data.json(); | 
 |  |  |             // }).then(function(data) { | 
 |  |  |             //     console.log("confirmTrans-" + JSON.stringify(data)); | 
 |  |  |             //     document.getElementById("file").files[0].name = ''; | 
 |  |  |             //     enableButtonStatus(); | 
 |  |  |             //     refreshFiles();             | 
 |  |  |             //     return data.status; | 
 |  |  |             // }) | 
 |  |  |                | 
 |  |  |             AWSService.post(staticResources.updateUrl, JSON.stringify({ | 
 |  |  |                 "txId":transId, | 
 |  |  |                 "sfRecordId":"", | 
 |  |  |                 "isSuccess":isSuccess | 
 |  |  |             }), function(result){ | 
 |  |  |                 console.log("confirmTrans-" + JSON.stringify(data)); | 
 |  |  |                 document.getElementById("file").files[0].name = ''; | 
 |  |  |                 enableButtonStatus(); | 
 |  |  |                 refreshFiles();             | 
 |  |  |                 return data.status; | 
 |  |  |             }) | 
 |  |  |              | 
 |  |  |                 refreshFiles();    | 
 |  |  |             }, staticResources.token); | 
 |  |  |          | 
 |  |  |         } | 
 |  |  |         function calculateFileSize(fileObject) { | 
 |  |  |             if (fileObject.size > 20971520) { | 
 |  |  | 
 |  |  |             } | 
 |  |  |         } | 
 |  |  |         function uploadFileToAWS(data, size, fileName) { | 
 |  |  |             console.log("body=" + JSON.stringify({ 'file': data, "size": size, 'fileName': fileName })); | 
 |  |  |              | 
 |  |  |             fetch(uploadUrl, { | 
 |  |  |                 method: 'POST', | 
 |  |  |                 body: JSON.stringify({ 'file': data, "size": size, 'fileName': fileName }), | 
 |  |  |                 headers: { | 
 |  |  |                     'Content-Type': 'application/json', | 
 |  |  |                     'pi-token': staticResources.token | 
 |  |  |                 } | 
 |  |  |             }).then((data) => { | 
 |  |  |                 return data.json(); | 
 |  |  |             }).then(result => { | 
 |  |  |  | 
 |  |  |                 console.log("result" + JSON.stringify(result)); | 
 |  |  |                  | 
 |  |  |                 if (result.success == true) { | 
 |  |  |             console.log("body=" + JSON.stringify({ 'file': data, "size": size, 'fileName': fileName })); | 
 |  |  |             AWSService.post(uploadUrl, JSON.stringify({  | 
 |  |  |                 'file': data,  | 
 |  |  |                 "size": size,  | 
 |  |  |                 'fileName': fileName }) | 
 |  |  |                 , function(result){ | 
 |  |  |                     console.log("result" + JSON.stringify(result)); | 
 |  |  |                      | 
 |  |  |                     if (result.success == true) { | 
 |  |  |                     key = result.object; | 
 |  |  |  | 
 |  |  |                     Visualforce.remoting.Manager.invokeAction( | 
 |  |  | 
 |  |  |                                 confirmTrans(result.txId, 1); | 
 |  |  |                             } | 
 |  |  |                              | 
 |  |  |                             // window.location.reload(); | 
 |  |  |                                window.location.reload(); | 
 |  |  |                         }, | 
 |  |  |                         { escape: true } | 
 |  |  |                     ); | 
 |  |  |                     | 
 |  |  |                      | 
 |  |  |                     console.log('key' + key); | 
 |  |  |                 } else { | 
 |  |  |                     alertErrorMessage('上传失败请稍后再试!'); | 
 |  |  |                 } | 
 |  |  |             }).catch((error) => { | 
 |  |  |                 console.error('Error:', error); | 
 |  |  |             }) | 
 |  |  |             debugger | 
 |  |  |             }, staticResources.token); | 
 |  |  |          | 
 |  |  |         } | 
 |  |  |             // fetch(uploadUrl, { | 
 |  |  |             //     method: 'POST', | 
 |  |  |             //     body: JSON.stringify({ 'file': data, "size": size, 'fileName': fileName }), | 
 |  |  |             //     headers: { | 
 |  |  |             //         'Content-Type': 'application/json', | 
 |  |  |             //         'pi-token': staticResources.token | 
 |  |  |             //     } | 
 |  |  |             // }).then(function(data)  { | 
 |  |  |             //     return data.json(); | 
 |  |  |             // }).then(function(result) { | 
 |  |  |  | 
 |  |  |             //     console.log("result" + JSON.stringify(result)); | 
 |  |  |                  | 
 |  |  |             //     if (result.success == true) { | 
 |  |  |             //         key = result.object; | 
 |  |  |  | 
 |  |  |             //         Visualforce.remoting.Manager.invokeAction( | 
 |  |  |             //             '{!$RemoteAction.FileUploadController.saveFile}', | 
 |  |  |             //             fileName, key, result.txId, parentId, | 
 |  |  |             //             function (resultvalue, event) { | 
 |  |  |  | 
 |  |  |  | 
 |  |  |                              | 
 |  |  |             //                 //2. show file list | 
 |  |  |             //                 if (resultvalue.status == 'fail') { | 
 |  |  |             //                     alertErrorMessage(resultvalue.message); | 
 |  |  |             //                     //1. Confirm trans | 
 |  |  |             //                     confirmTrans(result.txId, 0);                               | 
 |  |  |             //                 } else { | 
 |  |  |             //                     alertErrorMessage('上传成功'); | 
 |  |  |             //                     confirmTrans(result.txId, 1); | 
 |  |  |             //                 } | 
 |  |  |                              | 
 |  |  |             //                 // window.location.reload(); | 
 |  |  |             //             }, | 
 |  |  |             //             { escape: true } | 
 |  |  |             //         ); | 
 |  |  |                     | 
 |  |  |             //         console.log('key' + key); | 
 |  |  |             //     } else { | 
 |  |  |             //         alertErrorMessage('上传失败请稍后再试!'); | 
 |  |  |             //     } | 
 |  |  |             // }).catch(function(error) { | 
 |  |  |             //     console.error('Error:', error); | 
 |  |  |             // }) | 
 |  |  |             // debugger | 
 |  |  |         // } | 
 |  |  |         function downPdf(fileUrl) { | 
 |  |  |             window.open(fileUrl,'_blank'); | 
 |  |  |         } | 
 |  |  | 
 |  |  |         } | 
 |  |  |     </style> | 
 |  |  |     <apex:form id="theForm"> | 
 |  |  |         <apex:actionFunction name="refreshFiles" action="{!refreshFiles}" reRender="pdf,uploadSection"/>       | 
 |  |  |         <apex:actionFunction name="refreshFiles" action="{!refreshFiles}" reRender="pdf,theForm"/>       | 
 |  |  |         <br/> | 
 |  |  |         <br/> | 
 |  |  |         <apex:pageBlock id="block"> |