cancel
Showing results for 
Search instead for 
Did you mean: 

Best way to do it Upload and save file to SAP MII server

former_member186805
Participant
0 Kudos


Hello Experts.

Could you please assist me on how to upload  and save file on MII server?

I am looking for best and non-complex method of doing it.

Thanks in advance

Accepted Solutions (1)

Accepted Solutions (1)

former_member186805
Participant
0 Kudos

Dear Experts,

Thanks for your valuable input!

Finally , I got his functionality working, but I need your assistance to make me correct where I am missing.

I got the BASE64 encoded string in a variable in JSP, now i am calling Transaction using Runner Call in JSP's response.sendRedirect()  Method, but seems BAS64 string is distorting in between, it gives me Blurred image when generated via Xacute Engine, but if i directly copy the BASE64 string generated in my JSP code and put in directly to the input parameter of the MII transaction that has only image saver, it generates image perfectly, do you have any idea or way to call trx from JSP?

or any idea how to correct distorted BASE64 encoded string?

former_member185280
Active Contributor
0 Kudos

Try  something like this:

String str = new String(com.sap.lhcommon.util.Base64Util.encode(<YOUR_BYTE_ARRAY>), "UTF-8");

former_member186805
Participant
0 Kudos

Hello Christian,

Thanks for your time to reply..

i have already done the same, but still i get the garbled encoded string at TRX.

i defined charset in my JSP as well .

is it something related to URL-encoded ?

honestly i tried this as well, but did not get through. Please help.

former_member185280
Active Contributor
0 Kudos

I think its depreciated but doing this to the encoded string worked for me:

str  =  java.net.URLEncoder.encode(str , "UTF-8");

former_member186805
Participant
0 Kudos

Hi Christian,

java.net.URLEncoder  .do i need to import this package in MII environment? currently i am using 12.0

former_member185280
Active Contributor
0 Kudos

I dont think so. I don't have a 12.0 box to try but if the JRE is 1.3 you can't specify the encoding. Try it like this and see if it works:

str  =  java.net.URLEncoder.encode(str);

former_member186805
Participant
0 Kudos

Hi Christian,

this works perfect, Now I got the perfect working code . Thanks for your kind help and support!

former_member186805
Participant
0 Kudos

Hi Christian,

I have one issue :

I want to send BASE64 string of more than 1KB file to MII transaction.

from the above method mentioned, I get the correct BASE64 encoded String on Print ln

But same string when I try to use as parameter assign to Query template ( I am calling Xacute query template instead of Runner Call)

it shows Page not found or 404 error, any idea ? for less than 1 KB files its working correct for me. but not sure about more than 1 KB . I suspect an issue in Xacute Query template call [may be].

or Runner is best to use in this scenario. Kindly assist me .

Thanks

former_member185280
Active Contributor
0 Kudos

You are probably hitting a limit on the url size. You may have to do a http post and pass the string in the document body or possibly call the transaction via the web service interface. Can you use the content manager API like I do in the post below?

Regards,
Christian

former_member186805
Participant
0 Kudos

Hi Christian,

i get error here :

ContentManager.save("AppPhase2/WorkRequestDashboard/UploadedFiles/" + fileName , dataBytes , SessionHandler.getUser(request), false,null, false);

i believe something worng with the parameters here , i copied your code.

former_member185280
Active Contributor
0 Kudos

What was the error?

former_member186805
Participant
0 Kudos

Hi Christian,

it shows me Compilation Error in that line.

Please suggest me any alternative .

former_member185280
Active Contributor
0 Kudos

Going to need a little more detail than that to help out at this point.

former_member186805
Participant
0 Kudos

i am using MII workbench as IDE and not eclipse,, so i am not sure if i can see the compilation error details , currently it shows some compiling error.

this function works perfect

str  =  java.net.URLEncoder.encode(str , "UTF-8");


all good, problem comes when i send URL encoded BASE64 string in a Query template call via sendRedirect of Servlet JSP method.

for long enocded string, sendRedirect does not work and shows  "Page Not Found "

former_member185280
Active Contributor
0 Kudos

The server logs usually have some useful information.

Former Member
0 Kudos

Hi Ayush,

Good Day!! Can you kindly share the sample code here because I have a same requirement of uploading a file into MII 14.0 Server and it is giving permission error. As you said, it works perfect for you, so I am seeing some hope here..

Will be very very helpful if you please share a sample code.

Best Regards,

Ushinar

former_member186805
Participant
0 Kudos

---------------------------------------------JSP FILE-----------------------------------------------------------------------------------------

<html>
<head>
<meta http-equiv="Content-Type" content="text/html ; charset=iso-8859-1">
</head>
<body>
<%@ page import="java.io.*" %>
<%@ page import="com.sap.lhcommon.util.Base64Util" %>


<%
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength-totalBytesRead);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
File f = new File(saveFile);
FileInputStream fis = new FileInputStream(f);
fis.read(dataBytes);
if(fis!=null){fis.close();}

String base64String = new String(Base64Util.encode(dataBytes),"UTF-8");
String project_key=request.getParameter("pkey");
if (project_key == null){project_key="-1";}
String projectName=request.getParameter("pname");
String fileName=new String(saveFile);
  int extIndex=fileName.indexOf(".");
   fileName=fileName.substring(extIndex+1);
String fileOption=fileName.toUpperCase();
if(fileOption.equals("JPG") || fileOption.equals("PNG") || fileOption.equals("BMP") || fileOption.equals("JPEG") || fileOption.equals("GIF") || fileOption.equals("TIFF")){
  fileOption="JPG";


}
//String url=new String("http://serverName:50000/XMII/CM/FolderName/WorkRequestDashboard/Test.irpt?EncodedStr="+java.net.URLEncoder.encode(base64String));
//out.println(base64String);
//response.sendRedirect("http://serverName:50000/XMII/Illuminator?QueryTemplate=FolderName/WorkRequestDashboard/FileUpload_Xacute&Param.1="+java.net.URLEncoder.encode(base64String)+"&Param.2="+projectName+"&Param.3="+fileOption);
// response.sendRedirect("http://serverName:50000/XMII/Runner?Transaction=FolderName/WorkRequestDashboard/FILEUPLOAD&fileInput="+java.net.URLEncoder.encode(base64String)+"&projectName="+projectName+"&FileOption="+fileOption);
request.getRequestDispatcher("Test.irpt?EncodedStr="+java.net.URLEncoder.encode(base64String)+"&projectkey="+project_key+"&projectname="+projectName+ "&FileOption="+fileOption).forward(request,response);
                }

%>
</body>
</html>

--------------------------------------TEST.IRPT-----------------------------------------------------------------------------

<html>
<head>
<meta http-equiv="Content-Type" content="text/html ; charset=iso-8859-1">
<title>File Upload Form</title>
<script lang="javascript">
function upload(){
var fi=document.getElementById("file").value;
var pk=document.getElementById("projectkey").value;
var fop=document.getElementById("fo").value;
var pn=document.getElementById("pn").value;
//alert(fi+'\n'+pn+'\n'+fop);
var uploadObj=document.FileUpload.getQueryObject();
uploadObj.setParam(1,fi);
uploadObj.setParam(2,pk);
uploadObj.setParam(3,fop);
uploadObj.setParam(4,pn);
if(document.FileUpload.executeCommand()){
  document.getElementById("imsg").innerHTML="File Attached Successfully.";
  alert(document.FileUpload.getValueByName("Output",1));
  window.close();
}
else{
  alert(document.FileUpload.getLastError());
  return;
}
}
</script>
</head>
<input type="hidden" id="file" value="{EncodedStr}" >
<input type="hidden" id="projectkey" value="{projectkey}" >
<input type="hidden" id="fo" value="{FileOption}" >
<input type="hidden" id="pn" value="{projectname}" >
<body onload="upload();">
<span id="imsg"> </span>
</body >
<applet name="FileUpload" id="FileUpload" codebase="/XMII/Classes" code="iCommand" archive="illum8.zip" width="4" height="4" style="visibility: hidden">
<param name="QueryTemplate" value="FolderName/WorkRequestDashboard/FileUpload_Xacute">

</applet>
</html>

---------------------------------------------------------------------UPLOAD FORM .IRPT-----------------------------

<html>
<head>
<meta http-equiv="Content-Type" content="text/html ; charset=iso-8859-1">
<title>File Upload Form</title>

<script lang="javascript"  >
function validate(){
var val=document.getElementById("F1").value;
if(val==""){
  alert("Please select a File to upload");
  return false;
}
if(val.indexOf("docx") > 0 || val.indexOf("xls")> 0 || val.indexOf("xlsx")> 0 || val.indexOf("csv") >0 || val.indexOf("rtf") > 0 || val.indexOf("doc")> 0 || val.indexOf("ppt") > 0){
  alert("These file formats are not supported. Kindly try to upload an Image.")
  return false;
}
return true;
}
</script>
</head>

  <body>
<h2 align="center">{PNAME} </h2>
<form  method="POST"  enctype="multipart/form-data" action="uploadFile.jsp?pkey={PKEY}&pname={PNAME}" accept-charset="iso-8859-1" onsubmit="return validate();">
                <br>
          <center><table border="1" >
                   
                    <tr><td><b>Choose File:</b>
</td>
                    <td><input name="F1" id="F1" type="file"></td></tr>
                                        <tr><td colspan="2">
<p align="right"><input type="submit" value="Upload File" ></p></td></tr>
             <table>
     </cester>     
     </form>
</body>
</html>

PS: this is perfectly working code applicable for any format and size of images and Text FILES.

Former Member
0 Kudos

Thanks Ayush, Thank you so much..

I will get back to you once it works...

Regards,

Ushinar

former_member186805
Participant
0 Kudos

you need to write BLS to save Image to Content DB  via image saver action block.

Answers (2)

Answers (2)

former_member185280
Active Contributor
0 Kudos

This old thread might help:

Former Member
0 Kudos

For small files you can do it this way:

(1) Read file from storage into JavaScript variable

(2) Do a BASE64 encode of the String

(3) Send the BASE64 String to SAP MII

(4) Use the Image Writer action to save the BASE64 String to a file

The Image Writer requires a BASE64 string, but saves the unencoded data to MII.

Regards

Tobias

former_member186805
Participant
0 Kudos

Hi Tobias,

Thanks for you Reply!

Read file is URL of image from my local machine?

Former Member
0 Kudos

Hi Ayush,

reading the file from the local machine is the hardest task of the four steps.

There is no native way of how to read the file. You can use Flash, Java Applet or some other plugins (like ActiveX or JScript) to read the file.

Maybe someone else has a better solution for reading files.

Regards

Tobias

Former Member
0 Kudos

Hi Ayush,

What are the files you want to read from local system.

Regards,

Rohit Negi.

former_member186805
Participant
0 Kudos

Hi Rohit,

Images only.