Implementation of ANGULARJS upload function

Source: Internet
Author: User

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

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.