I'm new to programming, I've tried to read post on the net and try a few thing but nothing get me to the point. I try to have the option to upload a file on my webpage(exercice for study) I've made a rest service. When I make an html page with a regular form with input type file and submit button, it work fine. Also it redirect me to a page with a string from the response, but I want the string to be received as data to use in angular) So i've made a page to upload with angular, and since, the best i can get in my directory is a file named null with ~17kb size. So my question is how i can get with angular service with $http.post the same result as with a simple submit button ? a page that load with ui-router:
<h1>File Upload with Jersey</h1>
<form>
<p>
Select a file : <input type="file" name="file" onchange="angular.element(this).scope().uploadFile(file)"/>
</p>
<p>aaa{{file[0].name}}</p>
</form>
I use it with a controler as follow:
(function()
{
var module = angular.module("couponAdmin");
module.controller("fileCtrl", fileCtrlCtor)
function fileCtrlCtor(uploadService,$scope) {
$scope.uploadFile = function(item){
uploadService.doPost(item);
}
}
})();
and this is the service :
(function(){var module = angular.module("couponAdmin");
module.service("uploadService", uploadServiceCtor);
function uploadServiceCtor($http,typeService,getterService) {
this.doPost = function(item){
this.fd = new FormData();
this.fd.append("file", item);
$http.post( "http://localhost:8080/couponService/webapi/file/upload",this.fd,{
headers: {'Content-Type': undefined },
transformRequest: angular.identity
}).then(
function(promise){alert("success")
},
function(){
alert("failure on uploaaaad")
return;
})
}
}})()
here is the rest service:
package marcwe.couponService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;
@Path("/file")
public class UploadFileService {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
@FormDataParam("file") InputStream uploadedInputStream,
@FormDataParam("file") FormDataContentDisposition fileDetail) {
String uploadedFileLocation = "d://uploaded/" + fileDetail.getFileName();
// save it
writeToFile(uploadedInputStream, uploadedFileLocation);
String output = "File uploaded to : " + uploadedFileLocation;
GenericEntity<String> entity = new GenericEntity<String>("uploaded"){};
return Response.ok(entity,MediaType.TEXT_PLAIN).status(Status.ACCEPTED).build();
}
// save uploaded file to new location
private void writeToFile(InputStream uploadedInputStream,
String uploadedFileLocation) {
try {
OutputStream out = new FileOutputStream(new File(
uploadedFileLocation));
int read = 0;
byte[] bytes = new byte[1024];
out = new FileOutputStream(new File(uploadedFileLocation));
while ((read = uploadedInputStream.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}