Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用Angular和Java在浏览器的另一页中打开预览文件(pdf、docx、txt等)_Javascript_Java_Angular_Pdf_Ms Word - Fatal编程技术网

Javascript 如何使用Angular和Java在浏览器的另一页中打开预览文件(pdf、docx、txt等)

Javascript 如何使用Angular和Java在浏览器的另一页中打开预览文件(pdf、docx、txt等),javascript,java,angular,pdf,ms-word,Javascript,Java,Angular,Pdf,Ms Word,我正在使用Angular7和Java8开发一个web应用程序。我正在上传一个文件(pdf、docx、txt等),但问题是我无法通过RESTful web服务在浏览器的另一个页面中打开它。我得到错误415不支持的媒体类型。我尝试过这个帖子,但没有成功。以下是前端和后端的代码片段: 角度分量(通过传递路径和文件名的按钮调用的方法示例:C/doc/foo.pdf) 角度服务 downloadFile(path): Observable<Blob> { const url = '/d

我正在使用Angular7和Java8开发一个web应用程序。我正在上传一个文件(pdf、docx、txt等),但问题是我无法通过RESTful web服务在浏览器的另一个页面中打开它。我得到错误415不支持的媒体类型。我尝试过这个帖子,但没有成功。以下是前端和后端的代码片段:

角度分量(通过传递路径和文件名的按钮调用的方法示例:C/doc/foo.pdf)

角度服务

downloadFile(path): Observable<Blob> {
    const url = '/download';
    return this.http.post<Blob>(url, path, { responseType: 'blob' as 'json' });
}
Java服务

public byte[] getDownload(String pathFile) throws EgesException, IOException {
    Path path = Paths.get(pathFile);
    if(path.toFile().exists())
        return Files.readAllBytes(path);
    else
        throw new EgesException(EgesExceptionConstants.WARNING_ACT_NOTFOUND_EXCEPTION);
}

你不能。浏览器没有任何内置的方式来查看Word文档,因此,除非用户已将浏览器配置为使用某个插件打开它(99%的用户都没有这样做),否则浏览器将提示他们下载该文件

目前没有浏览器具有呈现Word文档所需的代码,据我所知,目前也没有用于呈现Word文档的客户端库

但是,如果您只需要显示Word文档,而不需要编辑它,则可以通过
使用Google文档查看器来显示远程托管的
.pdf/.doc/.docx/.txt

<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>

许多人使用这种方法来查看他们的文档文件。您可以在这里查看:


你应该将
产生的
媒体类型更改为其他类型,例如
应用程序/octet流
。我没有解释我不能将我的私人文档发送到我的比赛之外。这很安全,只是你可以在你的网站上将其用作iframe否,你必须公开链接,以便webapp可以访问它,意味着其他任何人都可以访问它。这不是用于私人文档预览的解决方案。
public byte[] getDownload(String pathFile) throws EgesException, IOException {
    Path path = Paths.get(pathFile);
    if(path.toFile().exists())
        return Files.readAllBytes(path);
    else
        throw new EgesException(EgesExceptionConstants.WARNING_ACT_NOTFOUND_EXCEPTION);
}
<iframe src="https://docs.google.com/gview?url=http://remote.url.tld/path/to/document.doc&embedded=true"></iframe>