下载来自 Java REST API in Angular 的 .doc
Download .doc that comes from a Java REST API in Angular
问题是当从angularjava接收到ResponseEntity时,它接收的是Blob,但是它下载的Word不正确,它是一个带有正确路径的word文件是
private aFile() {
this.rest.aFile(this.formData)
.subscribe(res => {
this.contenidoFile = res;
var blob = new Blob([this.contenidoFile], { type: 'application/octet-stream' });
saveAs(blob, "createdocument.doc");
}, (err) => {
console.error(err);
alert('Ha habido un error');
});
}
aFile(formData: FormData) {
return this.http.post(PATH_FILE, formData, {
responseType:'blob'
});
}
Java中的代码:
@PostMapping("/file")
public ResponseEntity<File> docFileV1(
@RequestParam("file") MultipartFile originalFile) {
return ResponseEntity.ok(docService.processDocFile(originalDocFile));
}
例如,原始 Word 的文本是 "Hello how is this a word document",而不是 angular 下载的 Word 文档的文本是
"C:\var\tmp\DocWork\bcc272d8-fdac-4384-97bc-1fdc5dd5736b\document.doc"
也就是说,我要下载的单词是转换成单词的路径,如何下载原单词而不是它的路径?
尝试使用此代码下载文件,而不是使用 saveAs:
var blob = new Blob([this.contenidoFile], { type: 'application/octet-stream' });
const e = document.createEvent("MouseEvents"),
a = document.createElement("a");
a.download = "data.docx";
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ["application/octet-stream", a.download, a.href].join(":");
e.initEvent("click", true, false);
a.dispatchEvent(e);
尝试如下,我用 .net 核心后端测试它
是 console.log 打印响应属性,如 'response.body'?
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class FileService {
constructor(private http: HttpClient) {
}
get(url: string) {
const formData = {};
return this.download(url, formData)
.subscribe((response) => {
console.log(response);
let file = new Blob([response.body], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"});
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
const name = "test";
window.navigator.msSaveOrOpenBlob(file, name);
} else {
const fileUrl = URL.createObjectURL(file);
const child = window.open(fileUrl);
}
});
}
download(url: string, formData): Observable<any> {
const requestOptions : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Accept": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
})
};
const request = new Request(requestOptions);
return this.http.post(url, formData, requestOptions);
}
}
问题是当从angularjava接收到ResponseEntity时,它接收的是Blob,但是它下载的Word不正确,它是一个带有正确路径的word文件是
private aFile() {
this.rest.aFile(this.formData)
.subscribe(res => {
this.contenidoFile = res;
var blob = new Blob([this.contenidoFile], { type: 'application/octet-stream' });
saveAs(blob, "createdocument.doc");
}, (err) => {
console.error(err);
alert('Ha habido un error');
});
}
aFile(formData: FormData) {
return this.http.post(PATH_FILE, formData, {
responseType:'blob'
});
}
Java中的代码:
@PostMapping("/file")
public ResponseEntity<File> docFileV1(
@RequestParam("file") MultipartFile originalFile) {
return ResponseEntity.ok(docService.processDocFile(originalDocFile));
}
例如,原始 Word 的文本是 "Hello how is this a word document",而不是 angular 下载的 Word 文档的文本是
"C:\var\tmp\DocWork\bcc272d8-fdac-4384-97bc-1fdc5dd5736b\document.doc"
也就是说,我要下载的单词是转换成单词的路径,如何下载原单词而不是它的路径?
尝试使用此代码下载文件,而不是使用 saveAs:
var blob = new Blob([this.contenidoFile], { type: 'application/octet-stream' });
const e = document.createEvent("MouseEvents"),
a = document.createElement("a");
a.download = "data.docx";
a.href = window.URL.createObjectURL(blob);
a.dataset.downloadurl = ["application/octet-stream", a.download, a.href].join(":");
e.initEvent("click", true, false);
a.dispatchEvent(e);
尝试如下,我用 .net 核心后端测试它
是 console.log 打印响应属性,如 'response.body'?
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { HttpClient, HttpHeaders } from '@angular/common/http';
@Injectable()
export class FileService {
constructor(private http: HttpClient) {
}
get(url: string) {
const formData = {};
return this.download(url, formData)
.subscribe((response) => {
console.log(response);
let file = new Blob([response.body], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"});
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
const name = "test";
window.navigator.msSaveOrOpenBlob(file, name);
} else {
const fileUrl = URL.createObjectURL(file);
const child = window.open(fileUrl);
}
});
}
download(url: string, formData): Observable<any> {
const requestOptions : any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Accept": "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
})
};
const request = new Request(requestOptions);
return this.http.post(url, formData, requestOptions);
}
}