SAPUI5 .xlsx 文件上传到 oData 创建流 - xstring 转换时出错
场景:在我的 UI5 应用程序中使用 sap.ui.unified.FileUploader,用户可以选择具有多个选项卡的 .xlsx 文件上传到后端 oData 服务。在后端 oData 服务中,我实现了 CREATE_STREAM 方法来接收数据。这里我想使用类“cl_fdt_xl_spreadsheet”创建Excel文件,读取工作表和数据等并进行一些处理。
问题:当调用后端 CREATE_STREAM 方法时,我假设 is_media_resource-value 中的数据采用 xstring 格式,但是当尝试使用 cl_fdt_xl_spreadsheet 创建对象时,我收到错误“无效文档格式”。
查看代码
<u:FileUploader id="xlsUploader"
name="iwbChecklistUpload"
fileType="xlsx"
maximumFilenameLength="250"
maximumFileSize="2000"
uploadOnChange="false"
uploadUrl="{uploadModel>/xlsUrl}"
filenameLengthExceed=".filenameTooLong"
fileSizeExceed=".fileSizeExceeded"
typeMissmatch=".fileTypeMismatch"
uploadComplete=".xlsComplete"
uploadProgress=".xlsProgress"
sendXHR="true"/>
控制器
onUpload: function(oEvent){
var oModel = this.getOwnerComponent().getModel();
var uploadModel = this.getView().getModel("uploadModel");
var xlsUploader = this.getView().byId("xlsUploader");
var xlsDomRef = xlsUploader.getFocusDomRef();
var xlsFile = xlsDomRef.files[0]; //Only one
if(xlsFile){
var oHeaderToken = new sap.ui.unified.FileUploaderParameter({
name: "x-csrf-token",
value: oEvent.getSource().getModel().getSecurityToken()
});
var oHeaderSlug = new sap.ui.unified.FileUploaderParameter({
name: "slug",
value: xlsFile.size + "|" + xlsUploader.getProperty("value")
});
xlsUploader.removeAllHeaderParameters();
xlsUploader.addHeaderParameter(oHeaderToken);
xlsUploader.addHeaderParameter(oHeaderSlug);
xlsUploader.upload()
}
后端oData CREATE_STREAM
DATA: lo_converter TYPE REF TO cl_abap_conv_in_ce,
lv_xstring TYPE xstring,
lv_string type string.
DATA : lo_excel_ref TYPE REF TO cl_fdt_xl_spreadsheet .
lv_xstring = is_media_resource-value.
TRY .
lo_excel_ref = NEW cl_fdt_xl_spreadsheet(
document_name = 'iwbcl.xlsx'
xdocument = lv_xstring ) .
CATCH cx_fdt_excel_core into data(oRef).
"Error caught here - Invalid document format
ENDTRY .
任何帮助将不胜感激。我确实以类似的方式上传图像,没有任何问题。
我的想法是,不知何故 is_media_resource-value 中的流不是 xstring (在我的文件上传器中使用 sendXHR="true" 可能会导致这种情况)。 我应该在某处指定 mime 类型吗?
Scenario: Using sap.ui.unified.FileUploader in my UI5 application, the user can select an .xlsx file with multiple tabs to upload to the back end oData service. In the back end oData service I have implemented CREATE_STREAM method to receive the data. Here I want to create the Excel file using class "cl_fdt_xl_spreadsheet", read the sheets and data etc. and do some processing.
Problem: When hitting the back end CREATE_STREAM method, I assume the data in is_media_resource-value is in xstring format BUT when trying to create the object with cl_fdt_xl_spreadsheet, I get an error "Invalid document format".
View Code
<u:FileUploader id="xlsUploader"
name="iwbChecklistUpload"
fileType="xlsx"
maximumFilenameLength="250"
maximumFileSize="2000"
uploadOnChange="false"
uploadUrl="{uploadModel>/xlsUrl}"
filenameLengthExceed=".filenameTooLong"
fileSizeExceed=".fileSizeExceeded"
typeMissmatch=".fileTypeMismatch"
uploadComplete=".xlsComplete"
uploadProgress=".xlsProgress"
sendXHR="true"/>
Controller
onUpload: function(oEvent){
var oModel = this.getOwnerComponent().getModel();
var uploadModel = this.getView().getModel("uploadModel");
var xlsUploader = this.getView().byId("xlsUploader");
var xlsDomRef = xlsUploader.getFocusDomRef();
var xlsFile = xlsDomRef.files[0]; //Only one
if(xlsFile){
var oHeaderToken = new sap.ui.unified.FileUploaderParameter({
name: "x-csrf-token",
value: oEvent.getSource().getModel().getSecurityToken()
});
var oHeaderSlug = new sap.ui.unified.FileUploaderParameter({
name: "slug",
value: xlsFile.size + "|" + xlsUploader.getProperty("value")
});
xlsUploader.removeAllHeaderParameters();
xlsUploader.addHeaderParameter(oHeaderToken);
xlsUploader.addHeaderParameter(oHeaderSlug);
xlsUploader.upload()
}
Back end oData CREATE_STREAM
DATA: lo_converter TYPE REF TO cl_abap_conv_in_ce,
lv_xstring TYPE xstring,
lv_string type string.
DATA : lo_excel_ref TYPE REF TO cl_fdt_xl_spreadsheet .
lv_xstring = is_media_resource-value.
TRY .
lo_excel_ref = NEW cl_fdt_xl_spreadsheet(
document_name = 'iwbcl.xlsx'
xdocument = lv_xstring ) .
CATCH cx_fdt_excel_core into data(oRef).
"Error caught here - Invalid document format
ENDTRY .
Any help will be much appreciated. I do upload images in a similiar fashion and there are no problems there.
My thoughts are that somehow the stream in is_media_resource-value is not xstring (using sendXHR="true" in my fileuploader may cause this).
Should I specify a mime type somewhere ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们有一个非常相似的用例。我看到的唯一区别是我们有
useMultipart="false"
。激活多部分(默认)后,xstring
可能看起来有所不同,因为它不仅仅包含 XSLX 文件。当查看xstring
时,正确的XLSX文件将以50 4B 03 04
开头。请参阅文档了解有关
useMultipart
属性的更多信息。We have a very similar use case. The only difference I see is that we have
useMultipart="false"
. With multipart activated (the default) thexstring
probably looks different because it contains more than just the XSLX file. When looking at thexstring
a proper XLSX file will begin with50 4B 03 04
.See the docs for more information about the
useMultipart
property.