Html:
<script type= "Text/javascript" src= "Js/angular-file-upload.min.js" ></script>
<span class= "SPAN3" data-type= "logo" ng-click= "UploadFile ($event. Target)" > select Photo </span>
<span class= "Tips1" > Note: The photo size is limited to 5M or less </span>
<input type= "File" style= "Display:none" id= "file" accept= "image/*" onchange= "Filechange ();" Ng-file-select= " Onfileselect ($files) ">
<input type= "File" style= "Display:none" id= "File1" accept= "image/*" ng-file-select= "Onfileselect ($files)" >
Js:
Angular.module ("App", [' Angularfileupload '])
. Controller ("Upload", function ($scope, $http, $location, $upload) {
var limitsize = 5 * 1024 * 1024;
$scope. Onfileselect = function ($files) {
for (var i = 0; i < $files. length; i++) {
$scope. File = $files [i];
if ($scope. File.size <= limitsize) {
$scope. Upload = $upload. Upload ({
url:con.host+ "Upload",//con.host (the way you request the background method path "Upload" is your method)
File: $scope. File
}). Success (function (data) {
if (data.status! = ' success ') {
Alert ("Upload failed");
}else{
var map = {};
map[$scope. Type] = Data.path;
$scope. postparams.pt_image = Data.path;
var file = document.getElementById ("file"). Files[0];
$ ("#photo"). attr ("src", window. Url.createobjecturl (file));
}
});
}else{
Alert ("Image size must not exceed 5M");
}
}
}
$scope. uploadfile = function (target) {
$scope. Type = $ (target). attr ("Data-type");
if ($scope. Type = = ' logo ') {
$ ("#file"). Click ();
}else{
$ ("#file1"). Click ();
}
}
function Filechange () {
Alert ("File");
var file = document.getElementById ("file"). Files[0];
$ ("#photo"). attr ("src", window.) Url.createobjecturl (file));
}
});
Java:
@RequestMapping (value= "/upload", Method=requestmethod.post)
public void DoAction (multiparthttpservletrequest req, httpservletresponse Res) {
Jsonobject obj = new Jsonobject ();
Multipartfile file = req.getfile ("file");
System.out.println (file+ "=file");
PrintWriter out = null;
try {
out = Res.getwriter ();
} catch (IOException e) {
E.printstacktrace ();
}
Out.print (Savefilepic (file,req));
}
Private Jsonobject Savefilepic (multipartfile file,multiparthttpservletrequest req) {
Jsonobject obj = new Jsonobject ();
if (file = = null) {
Obj.put ("status", "fail");
Obj.put ("Reason", "file is null");
return obj;
}
String fileName = File.getoriginalfilename ();
Get the name extension of a picture
String ExtensionName = FileName
. substring (Filename.lastindexof (".") + 1);
Extension to determine if a picture
if (! Fileutils.picextcheck (ExtensionName)) {
Obj.put ("status", "fail");
Obj.put ("Reason", "file is null");
return obj;
}
String FilePath = Req.getsession (). Getservletcontext (). Getrealpath ("/") + "upload/"//Here is the project fetch path
+ fileutils.getrandomfilename () + "." +extensionname; Here is a random get file name plus file suffix name
File path = new file (FilePath);
try {//create path
if (!path.exists ()) {
Path.mkdirs ();
}
File.transferto (path);
}
catch (Exception e) {
E.printstacktrace ();
}
Obj.put ("Status", "Success");
String paths = path.tostring ();
System.out.println (paths+ "---------------");
Obj.put ("Path", Paths.split ("Secxpen") [1].replace ("/", "\ \"));
return obj;
}
Implementation of ANGULARJS upload function