Sapui5使用上传收藏从桌面上传附件并将其写入网关

时间:2017-07-03 13:01:02

标签: javascript file-upload odata sapui5

我正在开发一款Travel应用程序。 感谢这个社区的帮助,现在我可以看到每个旅行者都存在的附件。 我现在要从桌面上传新的一个并将其保存在后端。 后端服务alredy存在。 我尝试上传文件,看到其他类似问题,但它们对我不起作用。

上传附件的代码视图控制器:

		onChange: function(oEvent) {
			var oUploadCollection = oEvent.getSource();
			// Header Token
			var oCustomerHeaderToken = new UploadCollectionParameter({
				name: "x-csrf-token",
				value: "securityTokenFromModel"
			});
			oUploadCollection.addHeaderParameter(oCustomerHeaderToken);
			MessageToast.show("Event change triggered");
		},

// Upload Function

		onUploadComplete: function(oEvent) {
  //    var oData = this.getView().byId("UploadCollection").getModel().getData();
	//		var aItems = jQuery.extend(true, {}, oData).items;
	//		var oItem = {};
	//		var sUploadedFile = oEvent.getParameter("files")[0].fileName;
    
			var sUploadedFile = oEvent.getParameter("files")[0].fileName;
			var location = oEvent.getParameter("files")[0].headers.location;
			var index = location.indexOf("/NavTravAttachment"); 
			// TravReqGetSet
		    var path = location.substring(index);
			var oCollection = oEvent.getSource();
			var collectionPath = "/TravReqGetSet('Pernr' + 'Reinr')/NavTravAttachment";
			var oTemplate = this.byId("UploadCollection").clone();
			// UploadCollection = oTemplate.getId();
			oCollection.bindAggregation("items", {
				path: collectionPath,
				template: oTemplate
			});

			setTimeout(function() {
				var oUploadCollection = this.getView().byId("UploadCollection");

				// this.getView().byId("UploadCollectionView").setUploadUrl("/sap/opu/odata/sap/ZTS0_TRAVEL_FIORI_SRV/NavTravAttachment");

				for (var i = 0; i < oUploadCollection.getItems().length; i++) {
					if (oUploadCollection.getItems()[i].getFileName() === sUploadedFile) {
						oUploadCollection.removeItem(oUploadCollection.getItems()[i]);
						break;
					}
				}

				// delay the success message in order to see other messages before
				MessageToast.show("Event uploadComplete triggered");
			}.bind(this), 8000);

		},

观点和片段:

...........
          <IconTabFilter id="UploadCollectionView" icon="sap-icon://attachment" iconColor="Neutral" design="Horizontal" text="Attachment" key="Attachment">
						<content>
							<core:Fragment id="AttachmentFrag"  fragmentName="ztravel.view.Attachment" type="XML"/>
						</content>
					</IconTabFilter>
					
..............      
<!-- fragment code -->

<core:FragmentDefinition xmlns:l="sap.ui.layout" xmlns:f="sap.ui.layout.form" xmlns:core="sap.ui.core" xmlns="sap.m"
	xmlns:mvc="sap.ui.core.mvc">
	<UploadCollection id="UploadCollection" maximumFilenameLength="55" multiple="true" change="onChange" fileDeleted="onFileDeleted"
		fileRenamed="onFileRenamed" fileSizeExceed="onFileSizeExceed" typeMissmatch="onTypeMissmatch" uploadComplete="onUploadComplete">
		<items>
			<UploadCollectionItem contributor="{contributor}" documentId="{documentId}" fileName="{fileName}" fileSize="{fileSize}"
				mimeType="{mimeType}" thumbnailUrl="{thumbnailUrl}" uploadedDate="{uploadedDate}" enableEdit="{enableEdit}" enableDelete="{enableDelete}"/>
		</items>
	</UploadCollection>
</core:FragmentDefinition>

错误是这样的: 未捕获的TypeError:无法读取未定义的属性'indexOf'(var location is undefined)

但是可以肯定的是,有很多事情我错了或者我没有做过,所以这个错误可能只是他们的直接后果。 看完其他类似的帖子后,我接受了这个功能。

先谢谢所有人,

Mavhart

0 个答案:

没有答案