PDF is corrupted after reconstructing it using URL.createObjectURL
up vote
0
down vote
favorite
I'm trying to pass a PDF file from the server and display it inside the browser, but the output comes out corrupted.
var blob = atob(data.Package);
console.log(blob);
var file = new Blob([blob], type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
Console log outputs something that appears to be correct PDF (just the beginning of output):

I'm saving a copy of the PDF on the server before transfer to make sure it is not corrupt and it works.
URL constructed with URL.createObjectURL(file) seems to be short:
blob:http://localhost:61631/ad749684-2992-4311-8b17-f382a7c687be
server side code:
Object doc = Convert.ToBase64String(_Document.DocumentStream.ToArray());
JObject response = new JObject(new JProperty("Package", JObject.FromObject(doc)));
return new AspResponse<Object>(response);
javascript
add a comment |
up vote
0
down vote
favorite
I'm trying to pass a PDF file from the server and display it inside the browser, but the output comes out corrupted.
var blob = atob(data.Package);
console.log(blob);
var file = new Blob([blob], type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
Console log outputs something that appears to be correct PDF (just the beginning of output):

I'm saving a copy of the PDF on the server before transfer to make sure it is not corrupt and it works.
URL constructed with URL.createObjectURL(file) seems to be short:
blob:http://localhost:61631/ad749684-2992-4311-8b17-f382a7c687be
server side code:
Object doc = Convert.ToBase64String(_Document.DocumentStream.ToArray());
JObject response = new JObject(new JProperty("Package", JObject.FromObject(doc)));
return new AspResponse<Object>(response);
javascript
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to pass a PDF file from the server and display it inside the browser, but the output comes out corrupted.
var blob = atob(data.Package);
console.log(blob);
var file = new Blob([blob], type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
Console log outputs something that appears to be correct PDF (just the beginning of output):

I'm saving a copy of the PDF on the server before transfer to make sure it is not corrupt and it works.
URL constructed with URL.createObjectURL(file) seems to be short:
blob:http://localhost:61631/ad749684-2992-4311-8b17-f382a7c687be
server side code:
Object doc = Convert.ToBase64String(_Document.DocumentStream.ToArray());
JObject response = new JObject(new JProperty("Package", JObject.FromObject(doc)));
return new AspResponse<Object>(response);
javascript
I'm trying to pass a PDF file from the server and display it inside the browser, but the output comes out corrupted.
var blob = atob(data.Package);
console.log(blob);
var file = new Blob([blob], type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
Console log outputs something that appears to be correct PDF (just the beginning of output):

I'm saving a copy of the PDF on the server before transfer to make sure it is not corrupt and it works.
URL constructed with URL.createObjectURL(file) seems to be short:
blob:http://localhost:61631/ad749684-2992-4311-8b17-f382a7c687be
server side code:
Object doc = Convert.ToBase64String(_Document.DocumentStream.ToArray());
JObject response = new JObject(new JProperty("Package", JObject.FromObject(doc)));
return new AspResponse<Object>(response);
javascript
javascript
edited Nov 9 at 20:49
asked Nov 9 at 20:24
AnKing
302512
302512
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45
add a comment |
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
It looks like the issue is because you need to convert the PDF data into an actual byte array, then pass that into the Blob constructor. Try this:
function convertToByteArray(input)
var sliceSize = 512;
var bytes = ;
for (var offset = 0; offset < input.length; offset += sliceSize)
var slice = input.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++)
byteNumbers[i] = slice.charCodeAt(i);
const byteArray = new Uint8Array(byteNumbers);
bytes.push(byteArray);
return bytes;
var blob = atob(data.Package);
console.log(blob);
var file = new Blob(convertToByteArray(blob), type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
It looks like the issue is because you need to convert the PDF data into an actual byte array, then pass that into the Blob constructor. Try this:
function convertToByteArray(input)
var sliceSize = 512;
var bytes = ;
for (var offset = 0; offset < input.length; offset += sliceSize)
var slice = input.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++)
byteNumbers[i] = slice.charCodeAt(i);
const byteArray = new Uint8Array(byteNumbers);
bytes.push(byteArray);
return bytes;
var blob = atob(data.Package);
console.log(blob);
var file = new Blob(convertToByteArray(blob), type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
add a comment |
up vote
1
down vote
accepted
It looks like the issue is because you need to convert the PDF data into an actual byte array, then pass that into the Blob constructor. Try this:
function convertToByteArray(input)
var sliceSize = 512;
var bytes = ;
for (var offset = 0; offset < input.length; offset += sliceSize)
var slice = input.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++)
byteNumbers[i] = slice.charCodeAt(i);
const byteArray = new Uint8Array(byteNumbers);
bytes.push(byteArray);
return bytes;
var blob = atob(data.Package);
console.log(blob);
var file = new Blob(convertToByteArray(blob), type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
It looks like the issue is because you need to convert the PDF data into an actual byte array, then pass that into the Blob constructor. Try this:
function convertToByteArray(input)
var sliceSize = 512;
var bytes = ;
for (var offset = 0; offset < input.length; offset += sliceSize)
var slice = input.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++)
byteNumbers[i] = slice.charCodeAt(i);
const byteArray = new Uint8Array(byteNumbers);
bytes.push(byteArray);
return bytes;
var blob = atob(data.Package);
console.log(blob);
var file = new Blob(convertToByteArray(blob), type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
It looks like the issue is because you need to convert the PDF data into an actual byte array, then pass that into the Blob constructor. Try this:
function convertToByteArray(input)
var sliceSize = 512;
var bytes = ;
for (var offset = 0; offset < input.length; offset += sliceSize)
var slice = input.slice(offset, offset + sliceSize);
var byteNumbers = new Array(slice.length);
for (var i = 0; i < slice.length; i++)
byteNumbers[i] = slice.charCodeAt(i);
const byteArray = new Uint8Array(byteNumbers);
bytes.push(byteArray);
return bytes;
var blob = atob(data.Package);
console.log(blob);
var file = new Blob(convertToByteArray(blob), type: "application/pdf" );
const fileURL = URL.createObjectURL(file);
window.open(fileURL);
answered Nov 9 at 20:59
jtate
1,11711023
1,11711023
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
add a comment |
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
it actually worked, thanks a lot!
– AnKing
Nov 9 at 21:14
great! glad I could help.
– jtate
Nov 9 at 21:23
great! glad I could help.
– jtate
Nov 9 at 21:23
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53232820%2fpdf-is-corrupted-after-reconstructing-it-using-url-createobjecturl%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What do you mean by "URL constructed with URL.createObjectURL(file) seems to be short"? What do you see when you go to the object url?
– jtate
Nov 9 at 20:35
when the url opens i see a blank page with some artifact in a middle
– AnKing
Nov 9 at 20:45