How to Download File

Hi,

I am using DMI approach to upload file. My Server side code to persist file in database is :

ISCFileItem fileItem = dsRequest.getUploadedFile(“file”);
attachment.setAttachmentname(fileItem.getFileName());
attachment.setAttachmenttype(fileItem.getContentType());
attachment.setAttachedfile(fileItem.get());
getAttachmentDAO().save(attachment);

Please let me know how to download the uploaded file.
My requirement is:

  1. Displaying file name in the list grid
  2. Whenever user click on the download icon or button, file will open in the available editor (e.g. doc file will open in MS Office word, image will open in MS Paint etc).

Thanks.

Hello Ashish,

When you set a field type of “binary” you’ll see SmartClient automatically show appropriate download icons in the field. At that point you are responsible for placing an InputStream into dsResponse.data under the fieldName of the binary field (“file” in your case) when the download operation occurs.

I have used InputStream but unable to download file.
Could you please let me know the sample code using DMI approach.

Below is the response from server :

//isc_RPCResponseStart–>[
{
endRow:2,
totalRows:2,
isDSResponse:true,
invalidateCache:false,
status:0,
startRow:0,
data:[
{
attachedfileStream:{},
attachmentdbid:115,
attachmentname:“test1.js”
},
{
attachedfileStream:{},
attachmentdbid:116,
attachmentname:“test.GIF”
}
]
}
]//isc_RPCResponseEnd

Thanks.

The server sends a special request to download a file when you press the download button - that’s the one to response to with an InputStream.

If you need more help, show all of te relevant files.

Thanks Isomorphic !!!

I am getting the below exception while downloading the file:

===================================================

=== 2010-03-04 21:08:24,028 [ng)'] WARN RequestContext - dsRequest.execute() failed:
java.lang.Exception: attempt to call unknown operationType: downloadFile
at com.isomorphic.datasource.DataSourceDMI.execute(DataSourceDMI.java:154)
at com.isomorphic.datasource.DataSourceDMI.execute(DataSourceDMI.java:62)

===================================================

Please let me know the reason. Might be I have missed something.

Thanks !!!

The error message is tell you what’s wrong in plain english - no DMI for the operationType “downloadFile”.

Thanks a lot !!!

Please let me know, what is the expected logic in operationType - “downloadFile” DMI service or DS file.

See the first reply in this thread.

Thanks.

Handled the required operation type but getting the below exception:

===============================================
com.isomorphic.servlet.IDACall top-level exception

java.lang.ClassCastException: java.lang.String cannot be cast to java.util.Map

Please let me know the reason, what am I missing or what I need to set in dsResponse?.

FYI,

  1. I have tried to set InputStream in the dsResponse.data.
  2. Set Attachment Object in dsResponse.data.
  3. Created Map map = new HashMap<Long, InputStream>(); and put InputStream in map with PK (Long)
  4. dsResponse.setData(dsRequest.getCriteria());

But every time getting the same Exception

Show the complete exception and all of the code involved in your DMI, including the DataSource definition.

Things would go much smoother if you didn’t need to be told this every time - even twice in an hour.

Thanks!!!

I have fixed the issue and it is working fine.

listGrid.removeSelectedData () is giving JS error if grid has binary attachment field

Hi,

I want to remove attachment record from listgrid and calling removeSelectedData () but I am getting below JS error.

There is no example to remove attachment.

this.removeAt is not a function
anonymous([Object attachedfile=Object 0=Object Class=Array])ISC_Grids.js (line 45)
anonymous(Object name=_1, Object name=_2)ISC_Core.js (line 3223)
anonymous()ComplexF…rmPane.js (line 617)
anonymous(Object DOMevent=Event mouseup eventType=mouseUp x=50 y=295, Object name=_2)ISC_Foundation.js (line 312)
anonymous(Object DOMevent=Event mouseup eventType=mouseUp x=50 y=295, Object name=_2)ISC_Foundation.js (line 314)
anonymous([ImgButton ID:isc_ImgButton_231] ns=Object name=delete size=24, “click”, Object name=_3, Object name=_4 value=false)ISC_Core.js (line 1437)
anonymous([ImgButton ID:isc_ImgButton_231] ns=Object name=delete size=24, “click”)ISC_Core.js (line 1298)
anonymous(mouseup clientX=50, clientY=295, Object name=_2)ISC_Core.js (line 1285)
anonymous(mouseup clientX=50, clientY=295, Object name=_2)ISC_Core.js (line 1276)
anonymous(function(), mouseup clientX=50, clientY=295)ISC_Core.js (line 1494)
anonymous()ISC_Core.js (line 57)
[Break on this error] The resource from this URL is not text: …3/isomorphic/system/modules/ISC_Grids.js

Thanks,
Ashish Chugh.

When posting JavaScript errors, always post the stack trace from Internet Explorer. Due to all the “anonymous” functions in the stack (a flaw in some versions of Firebug) your stack trace is useless.

Hi Isomorphic,

I have checked in the Smart Client console, using javascript:isc.showConsole() and getting following exception where I call removeSelectedData() on the listGrid

12:52:09.281:MUP5:WARN:Log:Error:
‘Object doesn’t support this property or method’
in https://localhost:7005/ER3/isomorphic/system/modules/ISC_Grids.js
at line 45
List.removeList(_1=>Array[1])
Canvas.removeSelectedData(_1=>undef, _2=>undef)
anonymous(Obj, undef)
“this.parentElement.parentElement.parentElement.parentElement.attachmentGrid.listGrid.removeSelectedData ();”
StatefulCanvas.handleActivate(_1=>Obj, _2=>undef)
StatefulCanvas.handleClick(_1=>Obj, _2=>undef)
EventHandler.bubbleEvent(_1=>[ImgButton ID:isc_ImgButton_237], _2=>“click”, _3=>undef, _4=>undef)
EventHandler.handleClick(_1=>[ImgButton ID:isc_ImgButton_237], _2=>undef)
EventHandler.$k5(_1=>[object Event], _2=>undef)
EventHandler.handleMouseUp(_1=>[object Event], _2=>undef)
EventHandler.dispatch(_1=>EventHandler.handleMouseUp(), _2=>[object Event])
anonymous(event=>undef)
“var returnVal=arguments.callee.$ch.isc.EH.dispatch(arguments.callee.$j2,event);return returnVal;”

This suggests that you have set saveLocally:true on a databound ListGrid when you are using a ResultSet / have called fetchData(). This setting is invalid. Either setSaveLocally:false and allow the grid to issue “remove” DataSource requests, or use an Array as the dataset, which will allow local changes.

Thanks a lot Isomorphic, it worked after setting saveLocally as false :slight_smile:

Hi Isomorphic,

Now, after calling removeSelectedData() I am getting the request on server and am able to delete the attachment, but it is not refreshing the grid after that.

Even I tried to write a callback in removeSelectedData() function, but that callback is also not getting called.

In my DMI I am calling dsResponse.setSuccess() also

Please suggest what I am missing here.

Use the troubleshooting steps from the FAQ in the SmartGWT forum about grids not updating properly - they also apply to SmartClient.