#javascript #pdf #browser #blob
#javascript #PDF #браузер #большой двоичный объект
Вопрос:
Я создаю PDF-файл и открываю его в новом браузере. Моя проблема в том, что слово «blob:» добавляется к URL-адресу
большой двоичный объект: http://localhost:3000/a0b859c9-57a0-40b7-a60f-9d2a72ab3c14
и я хотел бы быть ‘http://localhost:3000/a0b859c9-57a0-40b7-a60f-9d2a72ab3c14 ‘
Есть ли способ, которым я мог бы этого добиться? Мой код приведен ниже
const blob = new Blob([response.data], {type : 'application/pdf'});
var pdfFileUrl = URL.createObjectURL(blob);
window.open(pdfFileUrl);
URL.revokeObjectURL(pdfFileUrl);
update:
I tried below code as well. It also gives the same results.
''
const xhr = new XMLHttpRequest();
//Send the proper header information along with the request
// listen for `onload` event
xhr.onload = () => {
// process response
if (xhr.status == 200) {
// parse JSON data
// console.log( "Response Received")
// var pdfurl = URL.createObjectURL(xhr.response) ;
console.log(xhr.response)
window.open(URL.createObjectURL(xhr.response));
} else {
console.log(" In Error block")
console.error('Error!');
}
};
// create a `GET` request
xhr.open('POST', url);
xhr.responseType = 'blob'
xhr.setRequestHeader('Content-Type', 'application/json');
// send request
xhr.send(JSON.stringify(payload));''
Комментарии:
1.
blob:
в начале URL-адреса означает, что URL-адрес является URL-адресом объекта… Итак, если у вас нет файла, сохраненного с этим сумасшедшим URL, это не будет работать так, как вы думаете. Что такоеresponse.data
? PDF?… Тогда, пока у вас есть свойxhr.responseType = 'blob';
, вы должны быть в состоянии делать подобноеxhr.onload = function(){ open(URL.createObjectURL(this.response)); }
.2. Спасибо. Пробовал и это. Он также добавляет URL-адрес с помощью blob. xhr.onload = () => { // обработать ответ, если (xhr.status == 200) { // проанализировать данные JSON // console.log( «Получен ответ») // var pdfurl = URL.createObjectURL(xhr.response); окно console.log(xhr.response).open(URL.createObjectURL(xhr.response)); Результат: большой двоичный объект: localhost:3000/ab21216e-26f2-498e-83d6-4f5edc2dc358