Showing posts with label web. Show all posts
Showing posts with label web. Show all posts

Wednesday, January 20, 2021

How to post attachments using form to agent

I have a form with some text fields and I also needed to send attachments within same form.

Form is printed by agent and is processed by another agent written in LotusScript.

I spent some time working on solution and here it is.

The idea is to convert selected files to base64 on client side and then post data on submission and agent that process submission will conver base64 to file.

Here is a form, note that we run some logic when files are added

<form name="formName" method="post" action="agentName?openagent">
<input name="title" value="xxx">
<input type="file" name="files" multiple onchange="toBase64()">
</form>

Here is how we convert selected files to base64 and how we results as text fields to form (JS is not optimal, it can be done without jQuery)

function toBase64() {
  var files = document.querySelector('input[type=file]').files;

  var form = $("form");
  form.find("input[name^='filebase64']").remove(); // replace

  function readAndSave(file, index) {
    var reader = new FileReader();
	
    reader.addEventListener("load", function() {
      form.append("");
      form.append("");
    }, false);

    reader.readAsDataURL(file);
  }

  if (files) {
    [].forEach.call(files, readAndSave);
  }
}

Once form is submitted we have to read base64 items and convert them to file. There are at least 2 solutions: pure LS or Java/LS2J

a) LotusScript using NotesMIMEHeader
Private Function saveBase64AsFile(base64 As String, filePath As string) As Boolean
	On Error GoTo ErrorHandler

	Dim stub As NotesDocument
	Dim stream As NotesStream
	Dim item As NotesMIMEEntity
	Dim header As NotesMIMEHeader
	Dim emb As NotesEmbeddedObject
	Dim fileName As String
	Dim contentType As string
	Dim base64File As String

	fileName = StrRightBack(filePath, "\")
	contentType = StrRight(Strleft(base64, ";"), ":")
	base64File = StrRight(Base64, ",")
	
	Call scriptLog.loginfo(fileName)
	Call scriptLog.loginfo(contentType)
	
	Set stub = db.Createdocument()
	Set item = stub.CreateMIMEEntity("Body")
	Set header = item.createHeader("Content-Disposition")
	Call header.setHeaderVal({attachment; filename="} & fileName & {"})

	Set stream = app.NotesSession.CreateStream()
	Call stream.WriteText(base64File)
	Call item.SetContentFromText(stream, contentType, ENC_BASE64)

	Call stream.Truncate
	Call stream.Close
	Call stub.Closemimeentities(True)

	Set emb = stub.Getattachment(fileName)
	Call emb.Extractfile(filePath)
	
	Exit Function
ErrorHandler:
	Error Err, Error
End Function
b) Java with LS2J using native classses.
import java.util.Base64;
import java.io.IOException;
import java.nio.file.*;

public class Base64ToFile{

	public boolean convert(String base64String, String filePath) {
		try {
			byte[] decodedImg = Base64.getDecoder().decode(base64String.getBytes());
			Path destinationFile = Paths.get(filePath);
			Files.write(destinationFile, decodedImg);
			return true;
		} catch (IOException e) {
			e.printStackTrace();
		}
		return false;
	}
}
UseLSX "*javacon"
Use "Base64ToFile"

Class Base64ToFile
	Private jSession As JavaSession
	Private jClass As Javaclass
	Private jObject As JavaObject
	Private jError As JavaError
	
	Sub New()
		Set jSession = New JavaSession
		Set jClass = jSession.GetClass("Base64ToFile")
		Set jObject = jClass.Createobject()
	End Sub
	
	Public Function convert(base64 As String, filePath As String) As Boolean
		convert = jObject.convert(base64, filePath)
	End Function
End Class

Tuesday, March 22, 2011

have you already download new FireFox?

I've just updated it to version 4. It works fine. Unfortunately I expected it would run faster then 3.X but it did not happen. FF can't beat Chrome in this area. But there is another good news: all my plug-in work fine. Only firebug required re-installation.

Friday, June 04, 2010

JS/CSS/HTML compressors do you use?

JS => http://closure-compiler.appspot.com/home
CSS =>http://developer.yahoo.com/yui/compressor/
HTML => I've never used such tools at all, so would be interested in your suggestions :)

Wednesday, December 16, 2009

Use JSMin when do web developing

I started to use JSLint and JSMin just couple weeks ago but now I have to say that everybody must use it.
Today I used JSMin and I have to say that In my case it saves ~22% of space (original size was 28kb and after minimization it was 22kb).

So, now when you are ready to put your JS libraries to productive server, use JSMin always! It is really cool (at least I think so :) )

JSLint
http://www.jslint.com/

JSMin
http://www.crockford.com/javascript/jsmin.html

Thursday, December 03, 2009

Online JS validator

I have never used JS validator before (just because I did not think about it previously) . Today I decided to use one of them. I looked around and found JSLint. It is really good tools for validation of JS. I like it ! I recomend to use it to everybody, it gives very good results.

http://www.jslint.com/

Thursday, August 28, 2008

Redirect in WebQueryOpen

It looks like very simple task - redirect to another element in WebQueryOpen , but when I tried to do it it did not work ! I mean in WQO JavaScript does not work well (or probably at all).
So I found good solution for me, and I would like to share it:

We have to take current document and then change the value in $$HTMLHeader.

Saturday, May 03, 2008

cross language via URL

I finished one small project, where I had to add possibility to change language. I did not find the best solution/way how to do it in Internet. So, I did it how I wanted :)
In my case I had documents in database (=> HTML pages) with content. So, I added in every document (HTML's page) new fields with different content (for different language). I put content in different fields: content_en, content_it, content_ru... For opening document in every language I used next approach, look at these URL:
http://host/db.nsf/EN/F593D4CBD7E802FFC125742500638A22?opendocument
http://host/db.nsf/RU/F593D4CBD7E802FFC125742500638A22?opendocument
http://host/db.nsf/IT/F593D4CBD7E802FFC125742500638A22?opendocument

Then I added agent on QO event, and checked the name of "view" (EU/RU/IT), then took it and used it when I received content => "content_" & language.

Thursday, February 28, 2008

how we can close window in IE without confirmation?

solution is very simple, just keep in mind it, probably it will help you in future.

for IE.7 and for IE.6
input type="button" value="close" onClick="window.opener='_parent'; window.close();"

Tuesday, February 19, 2008

?searchview and ?ReadViewEntries

I had a task where I should receive data from view as XML(myView?ReadViewEntries) or JSON (myView?ReadViewEntries&outputformat=json). All was well, except one problem which I didn't solve. It was case when I used ?SearchView (myView?SearchView&query) to show data.

Actually I waned to do something like this:
myView?SearchView+ReadViewEntries&outputformat=json&query

And as I informed it is impossible in 5-6-7 versions. Guys on IBM's forum told me that it would be added in future version (probably R8, but who know:] ) and will be like this one ?SearchViewEntries

don't forher about it.

Monday, February 04, 2008

View -> JSON -> Excel report

Usually I (probably as all) used agent approach in web applications for making excel's reports. In my case I was asked to make some reports exporting all data from current view (reports should look like in view).

So I decided to use another approach (in my case it was JSON because I like it more). Here you could download the demo version how to receive data from LN database.
to receive all documents from view I used:
?ReadViewEntries&count=1000&Start=1&outputformat=json&ExpandView
to receive the name/title of columns I used:
?ReadDesign&Outputformat=json

and in the end of the story I used next code to create excel report

function
appExcel(){
if (
window.ActiveXObject){
var
xlApp = new ActiveXObject("Excel.Application");
xlBook = xlApp.Workbooks.Add();

xlBook.worksheets("Sheet1").activate;
var
XlSheet = xlBook.activeSheet;

XlSheet.cells(1,1).value = "value";
xlApp.visible = true;
}
}


This function works only in IE as far as I know.

Monday, January 28, 2008

several Domino->JS functions: StrLeft, StrRight, StrLeftBack, StrRightBack, StrMidleBack, StrMiddle

Here are a few JavaScript functions to mimic the @-function capabilities. I dont like to write every time my own functionals, just because they will be my and I will be proud for them. I don't like this :). I prefer to find some different solutions and take one that better (of course on my opinion)

function rightString(fullString, subString) {
if (fullString.indexOf(subString) == -1) {
return "";
}
else {
return (fullString.substring(fullString.indexOf(subString)+subString.length, fullString.length));
}
}

function leftString(fullString, subString) {
if (fullString.indexOf(subString) == -1) {
return "";
} else {
return (fullString.substring(0, fullString.indexOf(subString)));
}
}

You can find many other functions here site

Wednesday, December 19, 2007

is XML going to leave us? JSON is next?

SON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

JSON is built on two structures:

* A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
* An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.


here is a link to example JSON in Lotus Notes:
http://www.openntf.org/Projects/pmt.nsf/ProjectLookup/JSON%20LS

I will think about weak sides of every technology and will write later.

Enjoy

Tuesday, December 18, 2007

$V2AttachmentOptions

We can hide attachments using $V2AttachmentOptions field with Computed for Display type

What I usually do is set its value using the next formula:
@If(@IsDocBeingEdited; "1"; "0")

It will help to hide/show attachments in form.

p.s. dont forget about Computed for display