Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get bad request error #1

Open
ArunPrkash585 opened this issue Sep 1, 2015 · 2 comments
Open

get bad request error #1

ArunPrkash585 opened this issue Sep 1, 2015 · 2 comments

Comments

@ArunPrkash585
Copy link

I'm new to angular,i try to uploading files but i get bad request error

@murygin
Copy link
Owner

murygin commented Sep 7, 2015

Is there any log output..?

@ArunPrkash585
Copy link
Author

This my client side part

upload me

myApp.service('fileUpload', ['$http', function ($http) {
this.uploadFileToUrl = function(file, uploadUrl){
var fd = new FormData();
fd.append('file', file);
$http.post(uploadUrl, fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})
.success(function(){
})
.error(function(){
});
}
}]);

myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){

$scope.uploadFile = function(){
    var file = $scope.myFile;
    console.log('file is ' + JSON.stringify(file));
    var uploadUrl = "/customers/17/artifacts";
    fileUpload.uploadFileToUrl(file, uploadUrl);
};

}]);
This my server side part

controller part

@RequestMapping(value = "/customers/{accountNumber}/artifacts", method = RequestMethod.POST)
void uploadCustomerArtifact(@PathVariable Long accountNumber, @RequestParam("artifact") MultipartFile uploadfile, HttpServletRequest request, HttpServletResponse response) {
log.info("RestController UploadFile Called");

    artifactService.uploadArtifacts(accountNumber, uploadfile, request, response);
}

service part

@transactional
void uploadArtifacts(Long accountNumber, MultipartFile uploadfile, HttpServletRequest request, HttpServletResponse response)
{
BufferedOutputStream stream
try {

        Validate.assertNotNull(accountNumber,"field.required","Customer account number is mandatory");

        Customer existingCustomer = customerRepository.findByAccountNumber(accountNumber);
        Validate.assertNotNull(existingCustomer, "not.exist", "Customer account number doesn't exist");

        Artifact myArtiFact = new Artifact();
        myArtiFact.name = uploadfile.getOriginalFilename();
        myArtiFact.type = uploadfile.getContentType();
        myArtiFact.content = uploadfile.bytes

        String contentType = request.getServletContext().getMimeType(FilenameUtils.getName(uploadfile.getOriginalFilename()));

        println "contentType : "+contentType

        existingCustomer.addArtifact(myArtiFact);

        customerRepository.save(existingCustomer);
        log.info("UploadArtifacts Success");

    }
    catch (Exception e) {
        throw e;
    }
    finally
    {
        if(stream != null)
        {
            stream.close();
        }
    }       
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants