4

Did Someone try to upload a image in angularjs with jasny-bootstrap extension?

I use http://jasny.github.io/bootstrap/javascript/#fileinput

That's the code

<div class="fileinput fileinput-new" data-provides="fileinput">
            <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
            <div>
              <span class="btn btn-default btn-file">
                <span class="fileinput-new">Select image</span>
                <span class="fileinput-exists">Change</span>
                <span class="fileinput-upload" ng-click="">Upload</span>
                <input type="file" name="...">
              </span>
              <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
            </div>
          </div>

I want to upload a file selected on the server when click Upload.

Some advice?

1 Answer 1

7

To use this methodology, you will need the following module: https://github.com/ghostbar/angular-file-model

that works fine

in view html file:

    <div class="fileinput fileinput-new" data-provides="fileinput">
                <div class="fileinput-preview thumbnail" data-trigger="fileinput" style="width: 200px; height: 150px;"></div>
                <div>
                  <span class="btn btn-default btn-file">
                  <span class="fileinput-new">Select image</span>
                  <span class="fileinput-exists">Change</span>
                     <input type="file" name="file" file-model="compose.myFile">
                  </span>
                  <a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
                </div>
              </div>

in a controller

    var file = new FormData();
    var file1 = $scope.compose.myFile;
    file.append('file',file1);
    PubServices.save(file, function(data) {
                     .....
          }, function(error){
            console.log(error);
          });

And Service

angular.module('App')
  .service('PubServices', function ($resource) {
    return $resource(
      'http://localhost:8080/appServer/rest/secure/domain', 
      {file:'@file'}, 
      { 
      save: { 
        method: 'POST', 
        params: {file:'@file'},
        transformRequest: angular.identity,
        /* Note the headers property */
        headers: {'Content-Type': undefined},
      } 
  });
})
Sign up to request clarification or add additional context in comments.

3 Comments

I don't get what does the address 'localhost:8080/appServer/rest/secure/domain' is for. Could you tell me please?
that is a address on my rest java server. You want to know how is composed my server method?
You need to use the directive angular-file-model github.com/ghostbar/angular-file-model

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.