Using the ng2-file-upload plugin to upload a file using a file object
I am using the ng2-file-upload in an Angular 4.x project - I am trying to pass a base64 string that can be used to pass this file directly into the FileUploader class - I cannot be sure if this is even possible looking at the documentation currently?
https://github.com/valor-software/ng2-file-upload
public onPhotoEditBase64(base64, position): void {
let file = this.dataURLtoFile(base64, 'cchq-export');
console.log('fileObj', file); // outputs File object in console
// unsure how to pass the `file` variable into the ng2-fileupload in the existing array to replace the existing image based on the `position` parameter
}
/**
* convert image base64 uri to file object
* @param string dataurl
* @param string filename
* @returns {File}
*/
public dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
// fix for Internet Explorer
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
let blob = <any>{};
blob = new Blob([u8arr], { type:mime });
blob.lastModifiedDate = new Date();
blob.name = filename;
return blob;
} else {
return new File([u8arr], filename, {type:mime});
}
}
javascript angular promise base64 filereader
add a comment |
I am using the ng2-file-upload in an Angular 4.x project - I am trying to pass a base64 string that can be used to pass this file directly into the FileUploader class - I cannot be sure if this is even possible looking at the documentation currently?
https://github.com/valor-software/ng2-file-upload
public onPhotoEditBase64(base64, position): void {
let file = this.dataURLtoFile(base64, 'cchq-export');
console.log('fileObj', file); // outputs File object in console
// unsure how to pass the `file` variable into the ng2-fileupload in the existing array to replace the existing image based on the `position` parameter
}
/**
* convert image base64 uri to file object
* @param string dataurl
* @param string filename
* @returns {File}
*/
public dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
// fix for Internet Explorer
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
let blob = <any>{};
blob = new Blob([u8arr], { type:mime });
blob.lastModifiedDate = new Date();
blob.name = filename;
return blob;
} else {
return new File([u8arr], filename, {type:mime});
}
}
javascript angular promise base64 filereader
add a comment |
I am using the ng2-file-upload in an Angular 4.x project - I am trying to pass a base64 string that can be used to pass this file directly into the FileUploader class - I cannot be sure if this is even possible looking at the documentation currently?
https://github.com/valor-software/ng2-file-upload
public onPhotoEditBase64(base64, position): void {
let file = this.dataURLtoFile(base64, 'cchq-export');
console.log('fileObj', file); // outputs File object in console
// unsure how to pass the `file` variable into the ng2-fileupload in the existing array to replace the existing image based on the `position` parameter
}
/**
* convert image base64 uri to file object
* @param string dataurl
* @param string filename
* @returns {File}
*/
public dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
// fix for Internet Explorer
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
let blob = <any>{};
blob = new Blob([u8arr], { type:mime });
blob.lastModifiedDate = new Date();
blob.name = filename;
return blob;
} else {
return new File([u8arr], filename, {type:mime});
}
}
javascript angular promise base64 filereader
I am using the ng2-file-upload in an Angular 4.x project - I am trying to pass a base64 string that can be used to pass this file directly into the FileUploader class - I cannot be sure if this is even possible looking at the documentation currently?
https://github.com/valor-software/ng2-file-upload
public onPhotoEditBase64(base64, position): void {
let file = this.dataURLtoFile(base64, 'cchq-export');
console.log('fileObj', file); // outputs File object in console
// unsure how to pass the `file` variable into the ng2-fileupload in the existing array to replace the existing image based on the `position` parameter
}
/**
* convert image base64 uri to file object
* @param string dataurl
* @param string filename
* @returns {File}
*/
public dataURLtoFile(dataurl, filename) {
var arr = dataurl.split(','), mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
while(n--){
u8arr[n] = bstr.charCodeAt(n);
}
// fix for Internet Explorer
if (window.navigator && window.navigator.msSaveOrOpenBlob) { // IE workaround
let blob = <any>{};
blob = new Blob([u8arr], { type:mime });
blob.lastModifiedDate = new Date();
blob.name = filename;
return blob;
} else {
return new File([u8arr], filename, {type:mime});
}
}
javascript angular promise base64 filereader
javascript angular promise base64 filereader
asked Nov 19 '18 at 13:59
ZabsZabs
5,49138126219
5,49138126219
add a comment |
add a comment |
0
active
oldest
votes
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',
autoActivateHeartbeat: false,
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
});
}
});
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%2f53376255%2fusing-the-ng2-file-upload-plugin-to-upload-a-file-using-a-file-object%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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%2f53376255%2fusing-the-ng2-file-upload-plugin-to-upload-a-file-using-a-file-object%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