jquery ajaxform Uploading a file returns a question without prompting information

Source: Internet
Author: User

When using jquery's Ajaxform plugin for AJAX submission forms and uploading files, the return type Datatype:json but behind the scenes by writing out a JSON object, after execution did not enter the success function, but directly pop up a download window, The JSON string is now loaded for processing. It was later found that the solution was not to write JSON strings in JSON, but instead to "text/html" the JSON string into the textarea tag.

For example:

Response.setcontenttype ("Text/html;charaset=utf-8");

Response.getwriter (). Write ("<textarea>" +jsonstring+ "</textarea>");

This makes it possible to use JSON objects normally in JS.

Html:

<body>
<form id= "Uploadform" action= "${url}/uploadfile" method= "POST" >
English test: <input name= "FilePath" type= "file" ></input>
<button id= "Upload" > File detection </button>
</form>
</body>

$ (function () {

$ (' #upLoadForm '). Ajaxform ({
Success:function (data, StatusText, JQXHR, $form) {
alert (data);
}
});
});

Checkcontroller:

@RequestMapping (value = "/uploadfile", Method=requestmethod.post)
Public @ResponseBody void Processuploadd (httpservletresponse response,
@RequestParam (value = "FilePath") multipartfile file,httpsession HttpSession) {

String message= "No Chinese!" ";
Response.setcontenttype ("Text/html;charaset=utf-8");
try {
Response.getwriter (). write (message);
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}


/*string filename=file.getoriginalfilename ();//Get Upload file name
if (filename = = NULL | | "". Equals (FileName)
{
return null;
}

try{
FileManager FileSave = new FileManager ();
Get Upload Path
String Filepath=filemanager.getuploadfilepath ();
Filesave.savefilefrominputstream (File.getinputstream (), filepath,filename);
String content = filemanager.readtxtfile (filePath + "\ \" + filename);
List<dataformat> errorlist = checken.checken (content);
String info = "";
for (int i=0; i<errorlist.size (); i++) {
String cn = Errorlist.get (i). GETCN ();
String pub = Errorlist.get (i). GetErrorMessage ();
info = pub + "Chinese is:" + cn;
}
if (info!= "") {
Message=info;
}

}
catch (Exception e)
{
E.printstacktrace ();

return message;
} */
return message;
}

jquery ajaxform Uploading a file returns a question without prompting information

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.