AjaxFileupload, ajaxfileupload

Source: Internet
Author: User

AjaxFileupload, ajaxfileupload

AjaxFileuplaod is a plug-in for asynchronous Image Upload.

 

1. Introduce the plug-in:

<Script src = "$ {ctxStatic}/js/ajaxfileupload. js" type = "text/javascript"> </script>

<Script src = "$ {ctxStatic}/js/jquery. min. js" type = "text/javascript"> </script>
<Script src = "$ {ctxStatic}/js/bootstrap. min. js" type = "text/javascript"> </script>

2. jsp page

<Form action = "" class = "form-group" id = "data-form" encType = "multipart/form-data"/>

<Label class = "col-sm-2 control-label"> image address: </label>
<Div class = "col-md-4">
<Input class = "form-control" id = "adPicLink" name = "adPicLink"
Required type = "text" style = "width: 200px;"/>
</Div>
<Label class = "col-sm-2 control-label"> activity address: </label>
<Div class = "col-md-4">
<Input class = "form-control" id = "adLink" name = "adLink"
Required type = "text" style = "width: 200px;"/>
</Div>

<Label class = "col-sm-2 control-label"> activity status: </label>
<Div class = "col-md-4">
<Input class = "form-control" id = "adStatus" name = "adStatus" required
Type = "text" style = "width: 200px;"/>
</Div>
<Label class = "col-sm-2 control-label" id = "uploadLabel"> Image Upload: </label>
<Div class = "col-md-4">
<Input id = "upload" name = "upload" type = "file" multiple = ""/>
</Div>

</Form>

* EncType = "multipart/form-data": when data and images are uploaded together, you must add

3. js method

Url = "$ {ctx}/$ {moduleName}/update. json? ";

Var params = "adName =" + $ ("# adName "). val () + "& adPicLink =" + $ ("# adPicLink "). val () // I tried to use data: {"name": "name"} to pass the parameter. The backend obtains null. I think this is the only way to pass parameters, maybe there is better
Url = url + params;
$. AjaxFileUpload
(
{
Url: url, // the address of the upload Handler
Type: 'post', // Request Method
FileElementId: 'upload', // Id of the uploaded Image Tag

Data: data (I think it is useless. We recommend that you do not add it)
DataType: 'json', // type of data returned by the server. It can be xml, script, json, or html. If this parameter is left blank, jQuery will automatically judge it.
Success: function (result) // the server responds to the successful processing function.
Error: function (data) // server response failure processing function
}
)

4. configuration in the configuration file

<! -- When uploading files, SpringMVC needs to configure the MultipartResolver Processor -->
<Bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. CommonsMultipartResolver">
<! -- Specify that the total size of the uploaded file cannot exceed kb... note that the maxUploadSize attribute is not restricted to a single file, but to the sum of the total capacity of all files. -->
<Property name = "maxUploadSize" value = "800000"/>
</Bean>

5. Obtain the backend Controller

@ RequestMapping (value = "add", method = RequestMethod. POST)
Public ModelAndView add (@ FormModel TAdvertisement entity, HttpServletRequest request, @ RequestParam (value = "upload") MultipartFile file) throws Exception {
ModelAndView mav = new ModelAndView ("/demo2/show ");
String fileName = file. getOriginalFilename ();
String path = Thread. currentThread (). getContextClassLoader (). getResource (""). toString (); // obtain the project path
Path = path. replace ('/', '\'); // replace/\
Path = path. replace ("WEB-INF \ classes \", ""); // remove class \
Path = path. replace ("file:", ""); // remove the file:
Path = path. substring (1 );
Path = path + "images ";
Logger. debug ("path ========================>" + path );
Logger. debug ("fileName ==================>" + fileName );
File targetFile = new File (path, fileName );
File. transferTo (targetFile );
Logger. debug (path );
AdvertisementService. addItem (entity );
Mav. addObject ("result", "SUCCESS ");
Return mav;
}

Related Article

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.