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):



enter image description here



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);









share|improve this question























  • 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














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):



enter image description here



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);









share|improve this question























  • 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












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):



enter image description here



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);









share|improve this question















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):



enter image description here



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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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);





share|improve this answer




















  • it actually worked, thanks a lot!
    – AnKing
    Nov 9 at 21:14










  • great! glad I could help.
    – jtate
    Nov 9 at 21:23










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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);





share|improve this answer




















  • it actually worked, thanks a lot!
    – AnKing
    Nov 9 at 21:14










  • great! glad I could help.
    – jtate
    Nov 9 at 21:23














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);





share|improve this answer




















  • it actually worked, thanks a lot!
    – AnKing
    Nov 9 at 21:14










  • great! glad I could help.
    – jtate
    Nov 9 at 21:23












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);





share|improve this answer












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);






share|improve this answer












share|improve this answer



share|improve this answer










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
















  • 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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus