cancel
Showing results for 
Search instead for 
Did you mean: 

- Upload KM File

Former Member
0 Kudos

Hi guys,

I am having some problems trying to upload a file to KM using Web Dynpro APIs.

The problem is related to mime type inside KM.

Following the very helpful Blog by mr. Rohit Radhakrishnan I got uploaded some files but when I've found them in KM Content, the content of these files were not "recognized" as they were, for example, I uploaded a PDF file, however in KM Content that file cannot be opened as a PDF file,and it occured with XLS files, DOC files, etc.

Do you have some suggestions?

Thanks a lot,

Nelson Duarte.

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi Julian Reschke,

thank for your help.

I've take the first part of the one PDF file. I've seen the '%'character as a difference between the files

but, using a FC dos command I got too many differences between them.

(ORIGINAL FILE)

%PDF-1.4

%âãÏÓ

503 0 obj<</H[833 427]/Linearized 1/E 20058/L 186479/N 10/O 507/T 176371>>

endobj

(KM uploaded file)

PDF-1.4

%âãÏÓ

503 0 obj<</H[833 427]/Linearized 1/E 20058/L 186479/N 10/O 507/T 176371>>

endobj

The code that you asked me is the following:

//@@begin onActionNewUploadFile(ServerEvent)

String path = "/documents/Public Documents";

try{

IWDAttributeInfo attInfo = wdContext.getNodeInfo().getAttribute("fileData");

ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) wdContext.getNodeInfo().getAttribute("fileData").getModifiableSimpleType();

IWDClientUser wdClientUser = WDClientUser.getCurrentUser();

com.sap.security.api.IUser sapUser = wdClientUser.getSAPUser();

com.sapportals.portal.security.usermanagement.IUser ep5User = WPUMFactory.getUserFactory().getEP5User(sapUser);

ResourceContext context = new ResourceContext(ep5User);

RID rid = RID.getRID(path);

IResourceFactory factory = ResourceFactory.getInstance();

ICollection folder = (ICollection) factory.getResource(rid, context);

wdContext.currentContextElement().getFileName();

//location of the original file

String location = "c:

temp

";

wdComponentAPI.getMessageManager().reportSuccess("Nome do Arquivo: " + wdContext.currentContextElement().getFileName());

String fileName = "c:

temp

" + wdContext.currentContextElement().getFileName();

File file = new File(fileName);

wdContext.currentContextElement().setFileData(this.getBytesFromFile(file));

//temporary location

File fileout = new File("c:

temp

provisoria

" + wdContext.currentContextElement().getFileName());

//Create a temporary location for writing to a temporary location(as in Tutorial)

FileOutputStream out = new FileOutputStream(fileout);

out.write(wdContext.currentContextElement().getFileData());

out.close();

//From the temporary location read the file using an input Stream (as in Tutorial)

FileInputStream fin = new FileInputStream(fileName);

fin.read();

//this line it is not in Tutorial.

binaryType.setMimeType(new WebResourceType("pdf", "application/pdf", false));

//Using the Input Stream to write to repository (as Tutorial)

//Content content = new Content(fin, "byte", -1);

Content content = new Content(fin, binaryType.getMimeType().getHtmlMime(),-1);

wdComponentAPI.getMessageManager().reportWarning("Encoding text :" + content.getEncoding());

IResource newResource = folder.createResource(wdContext.currentContextElement().getFileName(), null, content);

fin.close();

file.delete();

}

catch (FileNotFoundException e) {

System.err.print("File Not Found Exception");

}

catch (IOException e){

System.err.print("I/O Exception");

}

catch (WDUMException e){

System.err.print("WDUM Exception");

}

catch (UserManagementException e){

System.err.print("User Management Exception");

}

catch (AccessDeniedException e){

System.err.print("Access Denied Exception");

}

catch (ResourceException e){

System.err.print("Resource Exception");

}

catch (WcmException wcm){

System.err.print("WcmException");

}

//@@end

Thank you for your attention and help.

Best wishes,

Nelson.

Former Member
0 Kudos

Remove

fin.read()

It consumes the first byte of the stream.

Answers (7)

Answers (7)

Former Member
0 Kudos

Hi Julian Reschke,

thank you very much, your last post solved the problem.

Best wishes,

Nelson.

Former Member
0 Kudos

Hi,

well, sorry for my English mistake, I should tell "character" instead of "char", sorry.

Regarding about "byte", I take it from a Tutorial that I took in SDN, and I've found it in some questions in the Forum. I'de like to tell you that the file was succesfully uploaded using it, but it is possible this it is the problem....what should I put instead of "byte", if possible, could you give an example?

Thank you for your attention.

Nelson.

Former Member
0 Kudos

I'm still not sure what the problem is. Are you saying the length of the file changes? Did you compare original file and the file in KM? What exactly are the differences? (please show the complete source code used for writing the file into KM).

The right content type for some unspecific binary content would be "application/octet-stream" (if a tutorial uses "byte" as content type, it needs to be fixed).

Best regards, Julian

Former Member
0 Kudos

Hi again guys,

still looking for a solution I’ve found that the file uploaded to KM lost some chars like ‘%’ and other that are related to the format (PDF, DOC, XLS, etc).

I think that it depends in some way of the following part of the code:

Content content = new Content(fin, “byte”,-1L);

IResource newResource = folder.createResource (wdContext.currentContextElement().getFileName(), null, content);

Do you have some suggestion about it?

Best wishes,

Nelson.

Former Member
0 Kudos

What do you mean by "lost some chars"?

Besides, "byte" is not a valid media type.

Former Member
0 Kudos

Hi guys,

first of all, I'd like to thank you for your help.

Secondly, I'd like to talk about your suggestions.

About the mr. Praveen Gudapati suggestion, I checked the configuration about MIME types and MIME handler service is activated.

Looking for the "Mime types" defined in KM configuration I found that the DOC, XLS, PDF extensions are present in that list.

Is it means that they've already setted? Another point is that when I do an upload manually files in KM I can open them without problem.

Could it confirm that KM sets are ok?

I am trying to understand and tryng to implement the other sugestions made by mr. Detlev Beutner, mr. Vinod Patil and mr. Julian Reschke.

I got the correct type icon adding the follwing line code:

binaryType.setMimeType(new WebResourceType("doc", "application/msword", false));

but the KM didn't identify, in this case, the content as an "DOC" file.

What should I do?

Best wishes,

Nelson.

S0019300750
Contributor
0 Kudos

Hi Nelson

You can use following code :

First find out the file extension using StringTokenizer and file name na d put it in fileExtension and use following code.

<i>IMimeHandlerService mh = (IMimeHandlerService) ResourceFactory.getInstance().getServiceFactory().getService("MimeHandlerService");</i>

While creating content use following code Here in is File InputStream object:

<i>Content content = new Content(in,mh.getMimetypeForExtension(fileExtension),in.available());

</i>

Former Member
0 Kudos

Note: do not use InputStream.available() unless you're sure that the implementing class really returns the length of the stream (as opposed to the number of bytes available).

In this particular case (Content constructor), the easiest thing is to let the system count the bytes for you (so just specify -1).

detlev_beutner
Active Contributor
0 Kudos

Hi Nelson,

You will have to set the mime type by yourself. Take this as a starting point:

IMimeHandlerService mimeService = MimeHandlerServiceFactory.getInstance();
IMime mime = mimeService.get(extensionString); // like "PDF" etc pp, see registered extensions (see link given by Praveen)
String mimeType = mime.getType();

Then use this value as value for the standard property contenttype.

Hope it helps

Detlev

0 Kudos

Hi,

Are your files having the right file extensions?

If so then, check the supported mime types by KM. Are the pdf, doc, xls extensions present in the MIME Types:

http://help.sap.com/saphelp_nw2004s/helpdata/en/19/14dfeed7b67a4d9b8406b0a56ebe37/frameset.htm

Also check if the MIME handler service is activated.

Greetings,

Praveen Gudapati

[Points are welcome for helpful answers]