java - How to upload a file(txt,pdf) and show it in the same webpage? -


i writing logic user uploads text file when user visits webpage first time(eg. in profile page). next time when user visits webpage should able find uploaded file link.

so far have been able upload file in user specific path , dwelling on how show in webpage.

i following mkyong problem find here action result in stream whereas in case valuestack has other action details.

code fragment :

1.action class

public string uploadresume(){      try{          if(uploadfile==null){             throw new nullpointerexception();         }      file file = new file("/users/shibasish/documents/","/"                      + generatetimestampid.generatetimestamp()+"/shibasish");     if (!file.exists()) {         if (file.mkdirs()) {             system.out.println("directory created!");         } else {             system.out.println("failed create directory!");         }     }     string filepath = file.getpath();     file filetocreate = new file(filepath, uploadfilefilename);     fileutils.copyfile(uploadfile, filetocreate);             }catch(nullpointerexception e){         addactionerror("please upload resume");     }     catch(exception e){         e.printstacktrace();     }     return "success"; } 

2.struts.xml

<action name="uploadresume" class="com.msventure.web.actions.completeprofileaction"                method="uploadresume">      <interceptor-ref name="defaultstack">     <param name="fileupload.allowedtypes">         text/plain,application/pdf,application/octet-stream     </param> </interceptor-ref>          <result name="success">/profile.jsp</result>         <result name="fail">/login.jsp</result>         <result name="index">/index.jsp</result>           <result name="login">/talent.jsp</result>     </action> 

jsp

<form id="login" name="login" method="post" action="signup"> <p>     <input type="submit" value="update" /> </p>   </form>     <s:if test="uploadfile neq null">   <!--<h4>download file - <s:a href="uploadfile">fileabc.txt</s:a></h4>-->   <a href="<s:property value="uploadfile" />" />   </s:if>   <s:form id="login" name="login" method="post" action="uploadresume"         enctype="multipart/form-data">     <s:file name="uploadfile" label="select file upload" size="40"/>     <s:submit value="submit" name="submit"/>     <!--<input type="button" value="search" id="resumeupload" />--> </s:form> 

please let me know in case not clear.

you can't return both jsp page and binary file.

when returning jsp, use dispatcher result, when returning file, use stream result.

after you've uploaded file, have 2 choices:

1. show file

post form in new tab target="_blank" , return stream result; user end having 2 tabs, 1 profile, other document.

2. show jsp file embedded in it

use <iframe> in profile page, , post form targeting iframe, target="name_of_the_frame_here".

there more complex ways (returning jsp, , composing serverside id you'll use on secondary action called iframe fetch document), i'd go solution #2.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -