在 SAPUI5 中上传文件并发送到网关服务器 [英] upload a file in SAPUI5 and send to Gateway server
问题描述
我在想办法发送上传的文件并将其发送到网关服务器时遇到了困难.我正在使用基本的 FileUploader 控件.
I am having difficulties trying to figure out a way to send uploaded file and send it to a Gateway server. I am using the basic FileUploader control.
***<u:FileUploader
id="fileUploader"
name="myFileUpload"
uploadUrl="upload/"
width="400px"
tooltip="Upload your file to the local server"
uploadComplete="handleUploadComplete"/>
<Button
text="Upload File"
press="handleUploadPress"/>***
在控制器中我有以下事件处理程序
And in the controller I have the following event handler
***handleUploadPress: function(oEvent) {
var oFileUploader = this.getView().byId("fileUploader");
oFileUploader.upload();
}***
我应该在 oFileUploader.upload() 之后添加什么代码以获得一个可以传递给我的 OData 服务的附件属性的 xstring
What code should I add after oFileUploader.upload() to have an xstring that I can pass to my attachment property of my OData srvice
谢谢
推荐答案
首先要做的是确保您拥有能够处理媒体类型和流的网关服务.要进行设置,您需要将处理文件内容的实体设置为媒体类型,并获得处理流的逻辑 (CREATE_STREAM).您可以在 此 SCN 博客.
The first thing to do is to make sure that you have a gateway service that is able to handle media types and streams. To set this up you need to set the entity that is handling the file content as a media type and get the logic in place that deals with streams (CREATE_STREAM). You can find more information on how to do this in this SCN blog.
在您的 UI5 应用程序中,您必须将上传控件的 URL 设置为例如/sap/opu/odata/sap/AWESOME_SERVICE/Customers('0000123456')/$value
以便文件由您刚刚实现的 CREATE_STREAM 方法处理.
In your UI5 application, you will have to set the URL of the upload control to e.g. /sap/opu/odata/sap/AWESOME_SERVICE/Customers('0000123456')/$value
so that the file is handled by the CREATE_STREAM method that you just implemented.
当上传最终发生时,你需要处理两个header参数;slug
和 CSRF 令牌
.slug 标头应设置为例如文件名,而 CSRF 令牌需要使用飞行前请求来检索.要设置标题,您可以使用以下内容:
When the upload is eventually taking place, you need to deal with two header parameters; the slug
and the CSRF token
. The slug header should be set to e.g. the filename, while the CSRF token needs to be retrieved using a pre-flight request. To set the headers, you could use something like this:
oFileUploader.addHeaderParameter(new FileUploaderParameter({
name: "x-csrf-token",
value: _csrfToken
}));
slug
标头参数可以类似的方式设置,并且应该包含识别文件的内容,例如文件名或 ID.
The slug
header parameter could be set in a similar way and should contain something that identifies the file, e.g. filename or id.
要确定 CSRF 令牌,您可以执行以下操作:
To determine the CSRF token, you could do something like this:
var _csrfToken = "";
jQuery.ajax({
url: "/sap/opu/odata/sap/AWESOME_SERVICE",
headers: {
"X-CSRF-Token": "Fetch",
"X-Requested-With": "XMLHttpRequest",
"DataServiceVersion": "2.0"
},
type: "GET",
contentType: "application/json",
dataType: 'json',
success: function(data, textStatus, jqXHR) {
_csrfToken = jqXHR.getResponseHeader('x-csrf-token');
}
});
使用正确的标头参数,将文件发送到正确配置的网关实体应该可以让您的文件上传.
With the right header parameters in place, sending the file to a properly configured gateway entity should do the trick to get your file uploaded.
这篇关于在 SAPUI5 中上传文件并发送到网关服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!