for iterating the upload file with comments

30 views
Skip to first unread message

ashok

unread,
Nov 18, 2012, 3:32:11 AM11/18/12
to scooter-...@googlegroups.com
hi friends,i prefered your uploadexample there simply we can upload a file with given the action address on the form submitition.
but here i want to upload documents with comments in my project but my i have some logical problem there,
i have two models patient and document, and i am given the relation to patient have many documents,
so is it possible to iterate the document if yes then pls can you show me the logic here, 
i am using this coding in my view show.jsp of patients folder like here comment field is brifly description about the document

<h2>Documents</h2>
<div id="Documents">
<%for (Iterator it = O.iteratorOf(O.allAssociatedRecordsOf("patient.documents")); it.hasNext();)
{
    Object document = it.next();%>
    <p>
  <p><b>Name of the uploaded file</b>: <%=W.get("document")%> </p>  <!-- here i am getting the null value on server -->
  <p>Click <%=W.labelLink("here", "/static/docs/" + W.get("document"))%> to view the uploaded document.</p> <!--here i am gettig an error like The server has not found anything matching the URI given. -->

        <b>posted on  </b><%=O.property(document, "uploaded_at")%>
    </p>

    <p>
        <b>Comment:</b>
        <%=O.property(document, "comment")%>
    </p>
    <hr/>
<%}%>
</div>

<h2>Add Documents</h2>
<%=W.errorMessage("document")%>

<%=F.formForOpen("patients", patient, "documents", "document")%> 
<p>
    <%=F.label("document")%><br />
  <input type="file" id="document_document" name="document" />
</p>

<p>
    <%=F.label("comment")%><br />
  <input type="text" id="document_comment" name="comment" value="<%=O.hv("document.comment")%>" size="40" /> 
</p>
  <input id="document_submit" name="commit" type="submit" value="Create" />&nbsp;&nbsp;&nbsp;<input type="reset"/>
<%=F.formForClose("documents")%> 




nd this code i am using in documentsController.java like 

 public String create() {
    ActiveRecord patient = Patient.findById(p("patient_id"));
    setViewData("patient", patient);
        try {
    UploadFile uf1 = pFile("document");
            uf1.writeTo(applicationPath() + "/static/docs");
            flash("notice", "You have successfully uploaded a file.");

            setViewData("document", uf1.getFileName());
        } catch (Exception ex) {
            flash("error", "There is a problem with upload.");
        }

        ActiveRecord newDocument = null;
        try {
    UploadFile uf1 = pFile("document");
            newDocument = Document.newRecord();
            newDocument.setData("document", p("document"));  // here it stores full path of the uploaded file from the directry 
            //newDocument.setData("document", p(uf1.getFileName())); 
            /*nd when i am using this line after commenting on //newDocument.setData("document", p("document")); then it is given an error on the server like There was a problem creating the comment record.
            Error in "/patients/1/documents": org.apache.jasper.JasperException: java.lang.IllegalArgumentException: The object which maps to key "document" for resource "documents" must be of RESTified type, but instead it is of "java.lang.String" type. */  
            newDocument.setData("comment", p("comment"));
            newDocument.setData("patient_id", p("patient_id"));
            newDocument.save();
            flash("notice", "Comment was successfully created.");

            return redirectTo(R.resourceRecordPath("patients", patient));
        }
        catch(Exception ex) {
            log.error("Error in create() caused by " + ex.getMessage());
            flash("error", "There was a problem creating the comment record.");
        }

        setViewData("document", newDocument);
        return forwardTo(viewPath("patients", "show"));
    }
    here i want whatever document we uploaded document's name stores in database nd shown on server 
   so pls can you tell me what mistake i did nd pls show me what logic might be there

John Chen

unread,
Nov 18, 2012, 10:38:43 PM11/18/12
to scooter-...@googlegroups.com
There are a few problems with your code. See my comments in blue color below.

But there is also an issue with Scooter's F.formForOpen API as it does not handle file upload mime type.

One way to workaround it is to view source of your browser code and paste the form code and add the mime type. Thus the form element which handles upload should be the following:

<form action="/care/patients/<%=patient.getRestfulId()%>/documents" class="add_document" id="add_document" method="POST" enctype="multipart/form-data">
  <p>
    <label for="document_document" >Document</label><br />
    <input type="file" id="document_document" name="document" value="" size="80" />
  </p>
  <p>
    <label for="document_comment" >Comment</label><br />
    <input type="text" id="document_comment" name="comment" value="" size="40" />
  </p>
  <input id="document_submit" name="commit" type="submit" value="Create" />&nbsp;&nbsp;&nbsp;
  <input type="reset"/>
</form>

You may use the attached files here for your testing.

John

On Sun, Nov 18, 2012 at 3:32 AM, ashok <ashok10...@gmail.com> wrote:
hi friends,i prefered your uploadexample there simply we can upload a file with given the action address on the form submitition.
but here i want to upload documents with comments in my project but my i have some logical problem there,
i have two models patient and document, and i am given the relation to patient have many documents,
so is it possible to iterate the document if yes then pls can you show me the logic here, 
i am using this coding in my view show.jsp of patients folder like here comment field is brifly description about the document

<h2>Documents</h2>
<div id="Documents">
<%for (Iterator it = O.iteratorOf(O.allAssociatedRecordsOf("patient.documents")); it.hasNext();)
{
    Object document = it.next();%>
    <p>
  <p><b>Name of the uploaded file</b>: <%=W.get("document")%> </p>  <!-- here i am getting the null value on server -->

I think you are iterating all documents for a patient here. So you need to use <%=O.property(document, "file_name")%>
Here I assume that file_name is a column in your "documents" table.
Or use
<%=O.property(document, "document")%> if "document" is the column name in the "documents" table.
 
  <p>Click <%=W.labelLink("here", "/static/docs/" + W.get("document"))%> to view the uploaded document.</p> <!--here i am gettig an error like The server has not found anything matching the URI given. -->

The same as above, change it to <%=O.property(document, "file_name")%>, assuming that you have stored your uploaded files under /static/docs/ directory in your upload controller.
 

The above line should be newDocument.setData("document", uf1.getFileName());
 
DocumentsController.java
show.jsp

ashok

unread,
Nov 19, 2012, 4:34:00 AM11/19/12
to scooter-...@googlegroups.com
 thank you so much for solving my doubt it is working nicely
Reply all
Reply to author
Forward
0 new messages