Thank you for being a valued client of CM First Group. If you have any technical issues or concerns, please open a ticket on support.cmfirstgroup.com, email CM First technical support at support@cmfirstgroup.com or call us on our 24/7 customer hotline at +1 (512) 623-7586.

Follow

How to implement file viewer in WebClient using Office Web Viewer

Reference

https://blogs.office.com/2013/04/10/office-web-viewer-view-office-documents-in-a-browser/#lGmJISRu1oWeLQ5b.97

https://support.office.com/en-us/article/View-Office-documents-online-1CC2EA26-0F7B-41F7-8E0E-6461A104544E

The file (Excel spreadsheet, Word document, and PowerPoint presentation) need to be accessible via the internet without using any port number.

  1.  Create a url field and set the control name of the field to FileURL.
  2. Create a button to trigger the file viewer event. Set the control name of the button to ViewFile:MainArea:template=FileViewer:default

FileViewer.ctrl will be attached in this article and it should be placed in your custom template Java project.

  1. Call an API that contains the following code:
// Generate a random number from 1 to 90
// Create a directory in the web context based on the generated random number
// Copy the file from your source directory to the newly created directory
// Create the url to access the new file
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.net.URLEncoder;
import java.nio.channels.FileChannel; {
    int random = (int)(Math.random() * 90 + & (3: ));
    com.adcaustin.webclient.IWebApp webApp = (com.adcaustin.webclient.IWebApp)(Object) fnc.getApp();
    javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest) webApp.getFromUserStorage("javax.servlet.http.HttpServletRequest");
    String path = req.getSession().getServletContext().getRealPath("/");
    java.io.File destPath = new java.io.File(path + random);
    java.io.File destFile = new java.io.File(path + random + "\\" + & (2: ));
    destPath.mkdir();
    String fileName = & (2: ).toString();
    String srcUrl = path + "/” + &(1:) + “/" + fileName;
    java.io.File srcFile = new java.io.File(srcUrl);
    FileChannel source = null;
    FileChannel destination = null;

    try {
        /**
         * getChannel() returns unique FileChannel object associated a file
         * output stream.
         */
        source = new FileInputStream(srcFile).getChannel();
        destination = new FileOutputStream(destFile).getChannel();
        if (destination != null && source != null) {
            destination.transferFrom(source, 0, source.size());
            String webPath = "http://mywebsite.com" + req.getContextPath() + "/" + random + "/" + fileName; &
            (4: ).fromString(URLEncoder.encode(webPath, "UTF-8"));
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (source != null) {
            try {
                source.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (destination != null) {
            try {
                destination.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
  1. Do a put to set the value of the URL.
  2. Optional: you can create a new API to delete the folders and files that are older than certain number of days.
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

Powered by Zendesk