找到你要的答案

Q:How to take data.name from success http post method in angular?

Q:如何从角成功HTTP POST方法采取data.name?

I read many questions and answers from my problem but I did't found solution. I try to download file and when I get file , that is Object .. how I can take name of that file?

my get method:

this.downloadFile = function(data) {
    return $http({
            method:'get',
            url:this.apiUploadFileUrl+"allFiles/"+data,
            responseType:'arraybuffer'
    });
};

on server side... a make response and Add file

File[] allFiles = projectFolder.listFiles();
                File oneFile = allFiles[id];
                path = oneFile.getAbsolutePath();
          InputStream in = new FileInputStream(new File(path));
          org.apache.commons.io.IOUtils.copy(in,       response.getOutputStream());
          response.flushBuffer();
          in.close();

and here i get file which I wont to download, but how to take name of the file ?

uploadFileRestServices.downloadAllFiles(data)
            .success(function(databack,response,status,data) {
                var file = new Blob([ databack ], {
                    type : 'application/csv'
                });
                var fileURL = URL.createObjectURL(file);
                var a         = document.createElement('a');
                a.href        = fileURL; 
                a.target      = 'filename';
                a.download    = "lll.txt";
                document.body.appendChild(a);
                a.click();              })
            .error(function() {
                alert("error");
    });

I got file but only I don't know how to take name of that object(file) ... OR... Because on server side I know filename , can I somewhere in response put file name(header or....) and how on angular side to take that attribut ???

I use AngularJS, REST, Spring ...

Thanks in advance for any help.

我从我的问题中读了许多问题和答案,但我没有找到解决办法。我尝试下载文件,当我得到文件,这是对象..我怎样才能拿到那个文件的名字?

我的方法:

this.downloadFile = function(data) {
    return $http({
            method:'get',
            url:this.apiUploadFileUrl+"allFiles/"+data,
            responseType:'arraybuffer'
    });
};

在服务器端…一个响应和添加文件

File[] allFiles = projectFolder.listFiles();
                File oneFile = allFiles[id];
                path = oneFile.getAbsolutePath();
          InputStream in = new FileInputStream(new File(path));
          org.apache.commons.io.IOUtils.copy(in,       response.getOutputStream());
          response.flushBuffer();
          in.close();

在这里我得到的文件,我不想下载,但如何采取文件的名称?

uploadFileRestServices.downloadAllFiles(data)
            .success(function(databack,response,status,data) {
                var file = new Blob([ databack ], {
                    type : 'application/csv'
                });
                var fileURL = URL.createObjectURL(file);
                var a         = document.createElement('a');
                a.href        = fileURL; 
                a.target      = 'filename';
                a.download    = "lll.txt";
                document.body.appendChild(a);
                a.click();              })
            .error(function() {
                alert("error");
    });

I got file but only I don't know how to take name of that object(file) ... OR... Because on server side I know filename , can I somewhere in response put file name(header or....) and how on angular side to take that attribut ???

我用AngularJS,休息,春天…

提前感谢任何帮助。

answer1: 回答1:

On the server side set the header. The default header name is 'Content-Disposition'.

@RequestMapping("/file")
public HttpEntity<byte[]> getFile() throws IOException {

    byte[] bytes = "random-file-content".getBytes();

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    header.set("Content-Disposition", "attachment; filename="
            + "original-file-name.bin");
    header.setContentLength(bytes.length);

    return new HttpEntity<byte[]>(bytes, header);
}

In Angular you can easily read the header like this:

angular.module('app', []).controller(
        'FileController',
        function($scope, $http) {
            $http.get('file').success(
                    function(data, status, headers, config) {
                        $scope.data = data;
                        $scope.filename = headers('Content-Disposition');
                    });
        });

在服务器端设置页眉。默认的页眉名称是“内容配置”。

@RequestMapping("/file")
public HttpEntity<byte[]> getFile() throws IOException {

    byte[] bytes = "random-file-content".getBytes();

    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    header.set("Content-Disposition", "attachment; filename="
            + "original-file-name.bin");
    header.setContentLength(bytes.length);

    return new HttpEntity<byte[]>(bytes, header);
}

在角,你可以很容易地读取头像这样:

angular.module('app', []).controller(
        'FileController',
        function($scope, $http) {
            $http.get('file').success(
                    function(data, status, headers, config) {
                        $scope.data = data;
                        $scope.filename = headers('Content-Disposition');
                    });
        });
answer2: 回答2:

You could add a custom HTTP header to the response to carry the filename. Simply set it on the response object on the server side and extract it from the response in the client.

$http.get(...).then(function(response) {
   var filename = response.headers('your-custom-header-name');
});

However, I think I'd rather make another request for the metadata.

你可以把文件名添加一个自定义HTTP头的响应。只需将其设置在服务器端的响应对象上,并从客户端的响应中提取它。

$http.get(...).then(function(response) {
   var filename = response.headers('your-custom-header-name');
});

不过,我想我宁愿对元数据提出另一个请求。

ajax  angularjs  http  servlets  httpresponse