Question

Preview File attchement (PDF) in content Control

  • 15 September 2016
  • 8 replies
  • 117 views

Hi all,

 

I want to display a preview of a PDF file stored in DB (attachment)

 

How can I pass the source in a content instead of url

I have the content (base64 of the file) and I know that content control have an url input but where can I find url output from files stored in DB

 

any idea or help plz ? 

 

thank you


8 replies

Hi Marwen

 

Out the box there is no way to display the contents of a file (from a DB) on a smartform. I think you would need to develop a custom control do this - I dont think I have ever seen one on the K2 Market. 

 

OK. My environment is little bit different than yours but I thought of sharing with you. Instead of saving pdf file in database, we save in the Sharepoint document library, so we have a document URL of the sharepoint library. If you can somehow findout the URL of the pdf document in the database, I think you can view the pdf file in the smartform with little trick.

 

1. Create a data-label in the smartform, name it Data Link.

 

2. In its properties window, click on the [...] of the style and create ConcatLink expression (attached) and assign to it.

or

2.  you can set the properties of the Data Link control on the Form Rule.

 

3.Check the literal in both the case.

 

 

Hi 

 

thank you for your answers, 

 

I solved this need by developping a simple asp.net page which connects to my smart object and passing to it  the id of the element and get the file stored in xml format  with the file content in base64.

the asp page renders a pdf viewer  then i use K2 action to set the url of the content control to the url of the viewer with its ID

 

thank you all

 

here is the code used 

 

SmartProperty fileMemo = smartObject.Properties["AttachementFile"];

// read the return properties of the SmartObject
if (fileMemo.Value != null)
{

var doc = new System.Xml.XmlDocument();
doc.LoadXml(fileMemo.Value);
var fileAsBase64 = doc.SelectSingleNode("/file/content").InnerText;
byte[] data = Convert.FromBase64String(fileAsBase64);

Response.ClearContent();
Response.OutputStream.Write(data, 0, data.Length);
Response.Flush();
Response.ContentType = "application/pdf";
}

 

Hi

 

Please when we run this code as a web form in visual studio, we get the following errors on debugging;

 

Error 3 The type or namespace name 'SmartProperty' could not be found (are you missing a using directive or an assembly reference?) c:usersk2_servicedocumentsvisual studio 2013Projects estwebapp estwebappPreview.aspx.cs *personal details removed* testwebapp

 

Error 4 The name 'smartObject' does not exist in the current context c:usersk2_servicedocumentsvisual studio 2013Projects estwebapp estwebappPreview.aspx.cs *personal details removed* testwebapp

 

Any help please?

 

Thank you

Here is a simple way that will work with K2 Five and later, in both on-premise and cloud versions. You won't need to download files, modify brokers, use fancy JavaScript, or bring SharePoint into the picture.

 

  1. Enable OData services in the Management portal.
  2. Expose the SmartObject that has the physical file saved in it.
  3. Set the URL of the Content Viewer control to the field of the SmartObject, after you've queried it using the OData query syntax. Here is an example of a query like that:
    https://YourK2Server/api/odata/resources/YourSmartObject(YourRecordID)/AttachmentFileFieldName
    WHERE
    • YourK2Server is the name of your K2 Server;
    • YourSmartObject is the name of your SmartObject;
    • YourRecordID is the numeric ID of your record, e.g. 1, 2, 333, etc; and
    • AttachmentFileFieldName is the name of the field in the SmartObject that stores the File Attachment.

Here is a working example, I've just changed the server name:

https://stevesk2server.com/api/odata/resources/UncategorisedInvoices(45)/AttachedPDF

Userlevel 2
Badge +4

In my case, the record was returned via

https://k2dev/api/odata/v3/ONRLogs(1045)

but the attachment was returned with

https://k2dev.lifeflight.org.au/api/odata/resources/ONRLog(1045)/PDF

 

Notice two changes, ONRLogs vs ONRLog and odata/v3 vs odata/resources.

 

Badge +3

Hi.

 

Thank you for sharing information. I have tried and it is not working with .msg(outlook) file.

is there a way to get link for the .msg file to open in browser or any IIS configuration needs to be done?

 

Thanks in advance.

 

Regards,

Thriveni

Just FYI, I know this post is 2 years old, but I tried this method today and it works REALLY slick.  

Reply