I have a query regarding file operations using JavaScript-
Scenario - My JS function calls a wcf service which returns the file content in the form of byte array or stream and the mime type. This byte array/stream needs to be converted to a file and which will be downloaded on user's machine.
Reference code -
var arr = "This is test content";
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], {
type: 'text/plain'
}));
a.download = "Test";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
The code works for only text files. Files with mime type other than text are corrupted. I understand that file operations are severly restricted at client side, but just to confirm - Is there anyway to convert byte array/stream to files such as Word,Excel, PDF and etc ?
I have a query regarding file operations using JavaScript-
Scenario - My JS function calls a wcf service which returns the file content in the form of byte array or stream and the mime type. This byte array/stream needs to be converted to a file and which will be downloaded on user's machine.
Reference code -
var arr = "This is test content";
var byteArray = new Uint8Array(arr);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], {
type: 'text/plain'
}));
a.download = "Test";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
The code works for only text files. Files with mime type other than text are corrupted. I understand that file operations are severly restricted at client side, but just to confirm - Is there anyway to convert byte array/stream to files such as Word,Excel, PDF and etc ?
Share Improve this question edited May 23, 2017 at 11:53 CommunityBot 11 silver badge asked Oct 14, 2015 at 11:24 Thomas MathewThomas Mathew 1,1516 gold badges15 silver badges30 bronze badges 3- Similar question here: stackoverflow./questions/27946228/… – Akhoy Commented Oct 14, 2015 at 12:38
- @Akhoy Thanks. Same URL as the reference code. As i said, text files are working but not word or pdf. – Thomas Mathew Commented Oct 15, 2015 at 3:41
- How to do this on .NET C#? I need to convert a File MIMEType octet-stream (format bytestream) to a excel is it possible too? – AbbathCL Commented Aug 26, 2021 at 23:26
1 Answer
Reset to default 2I acplish a similar goal with this. Pick up from where you have your byteArray, and try this:
var byteArray = new Uint8Array(arrayBuffer);
var a = window.document.createElement('a');
a.href = window.URL.createObjectURL(new Blob([byteArray], { type:'application/octet-stream' }));
// supply your own fileName here...
a.download = "YourFileName.XLSX";
document.body.appendChild(a)
a.click();
document.body.removeChild(a)
Setting contentType to "application/octet-stream" will acmodate any binary file type.
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744963743a4603547.html
评论列表(0条)