forked from qmacro/sapui5bin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ODataFileUpload.html
131 lines (119 loc) · 3.64 KB
/
ODataFileUpload.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<title>OData File Uploader Test</title>
<script id="sap-ui-bootstrap"
src="../resources/sap-ui-core.js"
type="text/javascript"
data-sap-ui-theme="sap_goldreflection"
data-sap-ui-libs="sap.ui.commons"
>
</script>
<script type="text/javascript">
/**
OData File Uploader Control
Extends FileUploader replacing the Form.Submit with an AJAX call,
the logic should work with any <input type="file"
**/
jQuery.sap.declare("ODataFileUploader");
jQuery.sap.require("sap.ui.commons.FileUploader");
sap.ui.commons.FileUploader.extend("ODataFileUploader", {
metadata : {
properties : {
"modelName" : "string",
"slug" : "string"
}
},
upload : function() {
var file = jQuery.sap.domById(this.getId() + "-fu").files[0];
try {
if (file) {
this._bUploading = true;
var that = this;
var _handleSuccess = function(data){
that.fireUploadComplete({"response": "Success: File uploaded to entity" });
that._bUploading = false;
};
var _handleError = function(data){
var errorMsg = '';
if (data.responseText[1]){
errorMsg = /<message>(.*?)<\/message>/.exec(data.responseText)[1];
}else{
errorMsg = 'Something bad happened';
}
that.fireUploadComplete({"response": "Error: " + errorMsg});
that._bUploading = false;
};
var oRequest = sap.ui.getCore().getModel(this.getModelName())._createRequest();
var oHeaders = {
"x-csrf-token": oRequest.headers['x-csrf-token'],
"slug": this.getSlug(),
};
jQuery.ajax({
type: 'POST',
url: this.getUploadUrl(),
headers: oHeaders,
cache: false,
contentType: file.type,
processData: false,
data: file,
success: _handleSuccess,
error: _handleError
});
jQuery.sap.log.info("File uploading to " + this.getUploadUrl());
}
} catch(oException) {
jQuery.sap.log.error("File upload failed:\n" + oException.message);
}
},
renderer : {
}
});
</script>
<script type="text/javascript">
//example shows how to post an image to the QF QANTAS Entity in the CarrierCollection
var sUrl = '/sap/opu/odata/IWFND/RMTSAMPLEFLIGHT/',
sCollection = 'CarrierCollection',
sCarrier = 'QF',
sPath = sCollection + "('" + sCarrier + "')";
var oModel = new sap.ui.model.odata.ODataModel(sUrl, true, 'developer', 'ch4ngeme');
oModel.read(sPath,null,null,true,null,null);
//model needs to be set to core so available to control
sap.ui.getCore().setModel(oModel);
var oFileUploader1 = new ODataFileUploader("upload_1", {
name: "test1",
uploadUrl: sUrl + sCollection,
slug: sCarrier, //Qantas
value: "",
width: "250px",
tooltip: "Upload your file to the local server.",
uploadComplete: function (oEvent) {
var sResponse = oEvent.getParameter("response");
if (sResponse) {
sap.ui.commons.MessageBox.show(sResponse);
var oImage = new sap.ui.commons.Image({
src: sUrl + sPath + "/$value",
alt: "uploaded image"
});
oImage.placeAt("target2");
}
}
});
var oLabel = new sap.ui.commons.Label({ text: "Fileuploader to the local server: ", labelFor: oFileUploader1});
oLabel.placeAt("target1");
oFileUploader1.placeAt("target1");
var oButton = new sap.ui.commons.Button({
text : "Upload",
press : function() {
oFileUploader1.upload();
}
});
oButton.placeAt("target1");
</script>
</head>
<body class="sapUiBody" role="application">
<div id="target1"></div>
<div id="target2"></div>
</body>
</html>