The struts2 + extjs file is uploaded, prompting you to solve the download problem.

Source: Internet
Author: User
 

This problem has always been a problem for beginners to learn extjs. I don't know what's going on. Maybe it's about ext source code, maybe it's about struts2. I 've searched many articles online, and there are also many solutions, I tried many of them, but now I have solved them. The extjs version I am using is 3.2. Use struts2 + ext to develop the upload module. The file is uploaded successfully, but the browser always prompts "Download "...... After reading the downloaded data, the returned JSON string is returned. However, the callback function at the front end is successful or fails to be executed. The solution is to directly write in the action

HttpServletResponse response = ServletActionContext.getResponse();response.setContentType("text/html;charset=UTF-8");

Also, the return value in action is changed from success to none.

Public String execute () throws ioexception {*************** Add the following part to the end. The struts configuration file is normal, and text/html is not required, no download prompt ...... (If you do not join this operation, there may be problems in the future. It is not involved yet. If you do not join this operation, let's talk about it ......) Httpservletresponse response = servletactioncontext. getresponse (); string MSG = "{success: true}"; response. getwriter (). Print (MSG); Return none ;}

<Param name = "contenttype"> text/html </param> is a solution. However, the solution is very special, that is, the action is not allowed to return any value to the foreground, in this way, the download box will not appear, but the prompt information is not sent to the front-end. How can I prompt whether the upload is successful or failed? So this method still does not work.

Finally, modify and add

<param name="contentType">text/html</param>

The reason for this is that, if the configuration is the same as the first method, but it will be rewritten by the default contenttype, the first method will not work.
Finally, paste the relevant important code for reference.
1. Save action configuration

Public String save () {string oldimagename = request. getparameter ("oldimagename"); // whether the request has been uploaded. If yes, delete if (! "Noimage ". inclusignorecase (oldimagename) {file oldfile = new file (servletactioncontext. getservletcontext (). getrealpath ("/") + "uploadimages" + file. separator + oldimagename); oldfile. delete ();} system. out. println (oldimagename); // obtain a new name for the user's newly uploaded image. Try {user. setimage (writefile ("userphoto"); userservice. adduser (User);} catch (exception e) {e. printstacktrace (); message = E. getmessage (); success = false; return none ;}

2. configuration in the struts configuration file

<result type="json" name="none"><param name="contentType">text/html;charset=utf-8</param><param name="excludeProperties">    user.myQuestionses,user.messages,user.myNotes,user.taskPapers,    user.tasks,user.testPapers,user.articles  </param></result>

3. Ext page

Register: function (BTN) {This. Form. getform (). Submit ({waitmsg: 'loading. Please wait ...... ', Waittitle:' hint ', URL: 'json2/fileupload_save_json', method: 'post', scope: This, success: function (Form, Action) {This. setuser (action. result. user. image) ;}, failure: function (Form, Action) {Ext. MSG. alert ('hprompt ', 'System error. You may have entered an error. Please try uploading later! ');}});},


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.