微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 Angularjs/Spring 上传/下载 uint8array

如何解决使用 Angularjs/Spring 上传/下载 uint8array

我有一个使用 Angularjs 作为前端的 Spring 应用程序。

在前端,我读取了一个使用 Openpgpjs 编码的文件。加密过程后,我得到一个 Uint8Array 对象,我想保存到数据库中。

// encoded_file is the Uint8Array
var file = new File(encoded_file,"my_image.png",{type:"image/png",lastModified:new Date()})

FileService.uploadFile(file).then(function(fileObject){
    console.log(fileObject); 
}).catch(function(error){
    toastrService.error(error,"Failed to upload file"); 
});


this.uploadFile=  function (file) {
    var defer = $q.defer();
    var fd = new FormData();
    fd.append('file',file);
    fd.append('auth',true);
    
    $http.post('/files/upload',fd,{
        transformRequest: angular.identity,headers: {'Content-Type': undefined}
    }).then(function (response) {
        if (response.data && response.data.result){
            defer.resolve(response.data.entry);
        } else if(response.data) {
            defer.reject(response.data.message);
        } else {
            defer.reject();
        }
    },function (error) {
        defer.reject(error);
    });
    
    return defer.promise;
};

服务器接收请求如下,将二进制数据保存在数据库

@RequestMapping(method = RequestMethod.POST,value = "/upload")
public String uploadFile(@RequestParam("file") multipartfile file,@RequestParam("auth") Optional<Boolean> auth) throws Exception {
       // save file.getBytes() in the DB and return a uniqueID to the file
}

文件可通过 URL /files/raw/id 在我的应用程序中访问

@RequestMapping(path = "/raw/{fileId}",method = RequestMethod.GET,produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
public @ResponseBody byte[] getRawFile(@PathVariable String fileId,HttpServletResponse response) throws Exception {
    File f = fileService.getFile(fileId);
    if (f == null) {
        return null;
    }

    return fileService.getFileContent(f);

}

我有以下功能来下载文件

this.downloadFile=  function (guid) {
    var defer = $q.defer();
    var config = { responseType: 'arraybuffer' };
    $http.get('/files/raw/'+guid,config).then(function (response) {
        console.log(response)
        if (response && response.data){

           defer.resolve(response.data); 
        } else {
            defer.reject();
        }
    },function (error) {
        defer.reject(error);
    });
    
    return defer.promise;
};

问题是我下载文件时得到的 Uint8array 与我上传的不同。 如果我将 responseType 更改为文本。该数字与我上传的 uint8array 相匹配,但我怎样才能正确?

解决方法

我发现了问题。

我将上传文件代码更改如下:

// encoded_file is the Uint8Array
 var blob = new Blob([encoded_file.buffer],{type: $file.type});
 var file = new File([blob],$file.name);

FileService.uploadFile(file).then(function(fileObject){
    console.log(fileObject); 
}).catch(function(error){
    toastrService.error(error,"Failed to upload file"); 
});

下载功能如下:

var fileReader = new FileReader();
fileReader.onload = function(event) {
      arrayBuffer = event.target.result;
};
fileReader.readAsArrayBuffer(file.data);
fileReader.onloadend = function (e) { 
     var data = new Uint8Array(arrayBuffer) 
                    
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?