Need Help for Download file from Database

Hi ,

we are using the smartclient,struts2.0 and ibatis technologies in our
application.

My requirement is that i need to display files from database and once i click

on the filename(l am using link), it needs to be downloaded to local system.

Can anybody help for the above requirement?

I deliberately needs to be completed with in a day.Please help.

Thanks ,
Vamsikrishna.V

It’s right in the docs, always the best place to start when you need to get something done quickly. Look for fieldType “binary”, which renders view and download icons in a grid.

Hello,

I am trying to get the file download of a binary field to work as well. But, when it displays in a grid, it is showing as undefined. And, when I click the links that are auto-generated, the links are trying to load the following URL:

URL: ‘/dev/smartclient.form/undefined’

Here is the code I see in the dev console for these links:

<div cellclipdiv="true" style="overflow: hidden; width: 296px;"><nobr><img src="http://localhost:8080/dev/isoversion/6.5.1/isomorphic/skins/SmartClient/images/actions/view.png" style="cursor: pointer;" onclick="assetNotesGrid.viewRow(0)" suppress="TRUE" align="texttop" border="0" width="16" height="16"> <img src="http://localhost:8080/dev/isoversion/6.5.1/isomorphic/skins/SmartClient/images/actions/download.png" style="cursor: pointer;" onclick="assetNotesGrid.downloadRow(0)" suppress="TRUE" align="texttop" border="0" width="16" height="16"> undefined</nobr></div>

And, I’ve attached a screenshot showing what the gird looks like. Any idea why it is displaying as “undefined”. Did I miss some simple configuration? I just defined the field as binary like the docs suggest as follows and I’ve confirmed I have binary data in the database:

<field name="attachedFile" type="binary"   >
	<title>
	<spring:message code="label.an.attachedfile"/>
	</title>
</field>

As the docs for fieldType “binary” mention, there are two other related fields that are normally auto-generated if you use our tools to generate tables. If you don’t, those fields still need to exist and be populated. Your problem in this case is related to the absence of a filename to show in the grid or in the URL.

Can you clarify what you mean by “if you use our tools to generate tables”. We are using smartclient to do everything so I’m not sure what I would’ve missed along the way.

The Admin Console can generate database tables from DataSource files. When it does so, it adds those fields. If you made the table directly and didn’t add those fields, that’s the problem. If you populated the table directory and didn’t add values for those fields, then that’s the problem.

Ok, I didn’t realize you were talking about database tables in your last response. Makes senses now. And, yes, we do generate our own Database tables. So, I’ve got that working. It would probably be helpful if the documentation clarified those applications that generate their own tables need to map from their database tables to the smartclient datasource attributes you are auto-generating.

However, the link that is generated is appending the file name to the url. Is that by design so that the url is different from the normal url used for other smartclient operations and thus the browser will open it in a different window? Right now, it is causing a 404 error from our server. Does this mean we need to configure our server to send this request to the same server-side controller that handles all of our other smartclient operations despite the filename at the end of the url?

I noticed that the dsRequest looks like this as well:

22:42:15.939:INFO:Log:dsRequest for assetNotes = {operationType: "viewFile",
dataSource: "AssetNotes",
data: Obj,
callback: null,
downloadResult: true,
downloadToNewWindow: true,
showPrompt: false,
download_filename: "adapter-admin.txt",
oldValues: undef}

I can’t find any documentation about operationType:“viewFile”. For instance, it is not mentioned here: http://www.smartclient.com/docs/6.5.1/a/b/c/go.html#attr..DSRequest.operationType . Is there any documentation or code examples available on recommended ways to processed these viewFile requests? I see a few posts

Yes, configure your server to send all requests underneath your controller’s path to the controller (this is the default configuration for the IDACall servlet). This actually a back-up mechanism for providing the filename to the browser (they don’t all reliably follow specs).

You don’t need to worry about operationType:“viewFile” if you just allow SmartClient to process it.

Ok, we’ve got our server configured properly. But, here is the next issue. When you say “just allow SmartClient to process it”, that only applies to those using the built-in IDACall servlet, right? Our server-side controller was built based on the logic in your example from the SDK in supplyItemOperations.jsp.

However, that jsp only handles ADD,UPDATE,FETCH, and REMOVE. In fact, it throws an error for any other type of operation. But, simply removing that error is only sending a null response back to the client. So, is there another example that shows how to process this “viewFile” request that we can follow?

Call dsRequest.execute() for this type of request. That causes the default handling to kick in, even if you are directly creating the RPCManager and using custom handling for other requests.

Hi, I tried dsRequest.execute() and got the following error:

java.lang.Exception: Operation type 'viewFile' not supported by this DataSource
	at com.isomorphic.datasource.DataSource.notSupported(DataSource.java:1134)
	at com.isomorphic.datasource.DataSource.executeDownload(DataSource.java:1116)
	at com.isomorphic.datasource.DataSource.execute(DataSource.java:758)
	at com.isomorphic.application.AppBase.executeDefaultDSOperation(AppBase.java:707)
	at com.isomorphic.application.AppBase.executeAppOperation(AppBase.java:664)
	at com.isomorphic.application.AppBase.execute(AppBase.java:497)
	at com.isomorphic.datasource.DSRequest.execute(DSRequest.java:1110)

So, perhaps I didn’t try to use dsRequest.execute() correctly? What I did was simply dsResponse = dsRequest.execute(). Let me know if there is something else I needed to do.

However, I have this working now after doing some hacking on my own. It’s not pretty but it works. Here is my implementation, please let me know if there are any problems with it:

if(operation.equals("viewFile") || operation.equals("downloadFile")){
			AssetNotesDTO anBean = new AssetNotesDTO(criteria);
			//get the attached file here to create a BusinessObject and then  put on the response from our database
			AssetNotesBO anBO = (AssetNotesBO) this.basicService
			.findById(AssetNotesBO.class, anBean
					.getAssetNotesID());
			if(anBO!=null){
				dsRequest.setDownloadFieldName("attachedFile");
				dsRequest.setDownloadFileName(anBO.getAttachedFileName());
				Map responseMap = new HashMap();
				responseMap.put("attachedFile" + "_filename", anBO.getAttachedFileName());
				responseMap.put("attachedFile" + "_filesize", anBO.getAttachedFileSizeInBytes());
				responseMap.put("attachedFile", new ByteArrayInputStream(anBO.getAttachedFile()));
				dsResponse.setData(responseMap);
			}

		}

So, to come full circle on this thread, I have one final question. You mentioned in your original response on this thread that binary field types “renders view and download icons in a grid” and that is working fine. But, I can’t figure out how to render view and download icons in a dynamicForm if we want to display to the user that a file is already attached. Of course, if there is no file attached, we’ll show them the upload control. But, if there is a file, we want to show them the view/download icons instead and give them a way to delete the current file. I’m guessing I’ll have to customize the “delete” part on a dynamicForm myself. But, is there a way to get those auto-generated view/download links to show up on a DynamicForm? I’ve been trying to make it happen to no avail so far.

Sorry, one other question on topic here. I noticed the view/download icons still display in the grid even if there is no file attached for a specific record. Is there an easy way to hide those view/download icons in that case?

Hi, well I’ve answered all my own questions. So, here is some sample code for displaying view/download/delete icons next to a “display” field in a dynamic form when there is a file attached. Notice the calls to datasource.viewFile and datasource.downloadFile which appear to be undocumented at this point for 6.5.1. Maybe someone else will find this useful and I think this is something that should eventually be built into the native smartclient product

//dipslay field that only shows if there is an attachedFile value 
//for this record
{name:"attachedFileDisplay",colSpan:3,type:"staticText",prompt:"<spring:message code="desc.an.attachedfilename"/>",
			shouldSaveValue:false,showIf:"return assetNotesRecord!=null && assetNotesRecord.attachedFile_filename!=null;",canEdit:false,
	         icons: [
		                {
		                	src:"[SKIN]/actions/view.png",
		                	click:"assetNotesEditor.dataSource.viewFile(assetNotesRecord);"
		                },
		                {
		                	src:"[SKIN]/actions/download.png",
		                	click:"assetNotesEditor.dataSource.downloadFile(assetNotesRecord);"
		                },

	                 {
	                	 src: "[SKIN]/actions/cancel.png",
	                	 click: function(){
		                
		        		    isc.confirm("<spring:message code="prompt.an.confirmremovefile"/>", 
		                            null,
		                            // Apply properties to the dialog to handle save / cancel clicks--%>                    
		                            {   
		                                buttons:[isc.Dialog.OK,        
		                                        isc.Dialog.CANCEL                              
		                                ],
		                                
		                                okClick : function () {
		        		    				removeAttachedFile();
		        		    				this.hide();
		                                },
		                                cancelClick : function () {
		                                   
		                                    this.hide();
		                                    isc.clearPrompt();
		                                }
		                            }
		                    	);
		                	
		                }
	                }
	          ]},


function removeAttachedFile(){
	
	assetNotesEditor.getField("attachedFileDisplay").setValue(null);
	if(assetNotesRecord!=null){
		assetNotesRecord.attachedFile_filename=null;
	}
	assetNotesEditor.markForRedraw();
	isc.Timer.setTimeout(
			function(){
				isc.say("<spring:message code="prompt.an.postremovefile"/>");
			}
	,0)
	
	
}

Also, here is a patch for displaying a blank cell in a list grid instead of view/download links when there is no attached file

//patch for displaying blank grid cell if no attached file
if (window.isc && isc.version.startsWith("6.5.1/")) {
	isc.ListGrid.getPrototype().addProperties({
	
	$30q:function(_1,_2,_3,_4,_5,_6){
		if(isc.isA.String(_1))return _1;
		var _7=_4[_2.name+"_filename"];
		if(_7!=null){
			var _8=isc.Canvas.imgHTML("[SKIN]actions/view.png",16,16,null,"style='cursor:"+isc.Canvas.HAND+"' onclick='"+_3.getID()+".viewRow("+_5+")'");
			var _9=isc.Canvas.imgHTML("[SKIN]actions/download.png",16,16,null,"style='cursor:"+isc.Canvas.HAND+"' onclick='"+_3.getID()+".downloadRow("+_5+")'");
			return _8+" "+_9+" "+_7
		}else{
			return null;
		}
	}
	})

}