Javascript AngularJS在新选项卡中打开PDF并更改文件名/url

Javascript AngularJS在新选项卡中打开PDF并更改文件名/url,javascript,angularjs,spring-mvc,Javascript,Angularjs,Spring Mvc,在我的AngularJS网站中,我想加载.pdf文档并在新的浏览器选项卡中显示它们。下面的代码可以工作,但有些事情让我感到困扰: createdObjectURL设置为文档和浏览器选项卡的标题。这意味着我的文档总是以类似于01d9bdca-9af2-43f0-a83f-f36fd82fd72的名称命名,这不是很体面。可见的URL也不好。而不是blob:http://localhost:8080/01d9bdca-9af2-43f0-a83f-f36fd82fd72f最好将我调用的URL放在ser

在我的AngularJS网站中,我想加载.pdf文档并在新的浏览器选项卡中显示它们。下面的代码可以工作,但有些事情让我感到困扰:

createdObjectURL设置为文档和浏览器选项卡的标题。这意味着我的文档总是以类似于
01d9bdca-9af2-43f0-a83f-f36fd82fd72
的名称命名,这不是很体面。可见的URL也不好。而不是
blob:http://localhost:8080/01d9bdca-9af2-43f0-a83f-f36fd82fd72f
最好将我调用的URL放在service.js中

我试图在HttpHeaders中处理文件名,但没有任何效果:(

另一件奇怪的事:如果我没有指定
{responseType:“arraybuffer”}
,PDF标题显示正确。但是没有内容,文档是空的

如果您对如何更改文件名和/或URL有任何帮助,我们将不胜感激。谢谢

JS控制器

DocumentService.getDocument().then(function(response) {
  $window.open(response, '_blank');
});
JS服务

return {
  getDocument : function () {
    return $http.get("document", {responseType: "arraybuffer"})
      .then(function(response) {
        var file = new Blob([response.data], {type: "application/pdf"});
        var fileURL = URL.createObjectURL(file);
        return fileURL;
      }
  }
}
@RequestMapping(value = "/document", method = RequestMethod.GET)
public ResponseEntity<byte[]> getDocument(){
  try {
    File file = new File("C:\\pathToFile.pdf");
    byte[] fileContent = new byte[(int) file.lenth()];

    HttpHeaders heraders = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "NameOfFile.pdf";
    headers.setContentDispositionFormData(filename, filename);  //doesn't change anything

    fileContent = File.readAllBytes(file.toPath());

    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(fileContent, headers, HttpStatus.OK);

    return response;
  } catch (FileNotFoundException e ) {
    System.err.println("File not found " + e);
  } catch (IOException e ) {
    System.err.pringln("Error while reading file " + e);
  }
}
春季MVC休息服务

return {
  getDocument : function () {
    return $http.get("document", {responseType: "arraybuffer"})
      .then(function(response) {
        var file = new Blob([response.data], {type: "application/pdf"});
        var fileURL = URL.createObjectURL(file);
        return fileURL;
      }
  }
}
@RequestMapping(value = "/document", method = RequestMethod.GET)
public ResponseEntity<byte[]> getDocument(){
  try {
    File file = new File("C:\\pathToFile.pdf");
    byte[] fileContent = new byte[(int) file.lenth()];

    HttpHeaders heraders = new HttpHeaders();
    headers.setContentType(MediaType.parseMediaType("application/pdf"));
    String filename = "NameOfFile.pdf";
    headers.setContentDispositionFormData(filename, filename);  //doesn't change anything

    fileContent = File.readAllBytes(file.toPath());

    ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(fileContent, headers, HttpStatus.OK);

    return response;
  } catch (FileNotFoundException e ) {
    System.err.println("File not found " + e);
  } catch (IOException e ) {
    System.err.pringln("Error while reading file " + e);
  }
}
@RequestMapping(value=“/document”,method=RequestMethod.GET)
公共响应getDocument(){
试一试{
File File=新文件(“C:\\pathToFile.pdf”);
byte[]fileContent=新字节[(int)file.lenth()];
HttpHeaders=新的HttpHeaders();
headers.setContentType(MediaType.parseMediaType(“application/pdf”);
字符串filename=“NameOfFile.pdf”;
headers.setContentDispositionFormData(文件名,文件名);//不更改任何内容
fileContent=File.readAllBytes(File.toPath());
ResponseEntity response=新的ResponseEntity(文件内容、标题、HttpStatus.OK);
返回响应;
}catch(filenotfounde异常){
System.err.println(“未找到文件”+e);
}捕获(IOE异常){
System.err.pringln(“读取文件时出错”+e);
}
}
短版:
当PDF文件作为字节数组加载并显示在带有AngularJS$window指令的新浏览器选项卡中时:是否有可能更改文件名和URL?

打开一个新选项卡,其中包含返回字节数组的资源的URL!类似于:

$window.open('localhost:xxx/api/document', '_blank');

打开一个新选项卡,其中包含返回字节数组的资源的url!类似于:

$window.open('localhost:xxx/api/document', '_blank');

你不能打开一个新的标签,上面有返回字节数组的资源的url吗?类似于
$window.open('localhost:xxx/api/document','u blank');
谢谢@DanielBornstrand,这很有效。现在窗口标题只是
document
,但是文档标题(如果文档被下载的话)是我指定的。这不是100%完美,但比以前好了很多,代码也少了:)我会把它作为答案发布的!:)你不能打开一个新的标签,上面有返回字节数组的资源的url吗?类似于
$window.open('localhost:xxx/api/document','u blank')谢谢@DanielBornstrand,这很有效。窗口标题现在只是
文档
,但文档标题(如果文档已下载)是我指定的。这不是百分之百的完美,但比以前好了很多,代码也少了:)那我就把它作为答案发布吧!:)