Click here to Skip to main content
15,888,521 members
Home / Discussions / JavaScript
   

JavaScript

 
QuestionAdding a list of files to the array and displaying them in the component Pin
Member 140077214-Oct-18 4:33
Member 140077214-Oct-18 4:33 
QuestionDate picker style not showing Pin
thepast24-Sep-18 8:21
thepast24-Sep-18 8:21 
AnswerRe: Date picker style not showing Pin
ZurdoDev24-Sep-18 8:58
professionalZurdoDev24-Sep-18 8:58 
GeneralRe: Date picker style not showing Pin
Iniciativa Box Funcional25-Sep-18 7:36
Iniciativa Box Funcional25-Sep-18 7:36 
QuestionRelation between null and zero. Pin
GKP199222-Sep-18 21:21
professionalGKP199222-Sep-18 21:21 
AnswerRe: Relation between null and zero. Pin
Richard Deeming24-Sep-18 8:03
mveRichard Deeming24-Sep-18 8:03 
GeneralRe: Relation between null and zero. Pin
GKP199224-Sep-18 17:37
professionalGKP199224-Sep-18 17:37 
QuestionTypeScript, Dropping Image and loading preview without posting, a working sample, FileReader, readAsDataUrl, reader.result Pin
jkirkerx19-Sep-18 7:55
professionaljkirkerx19-Sep-18 7:55 
I spent hours trying to figure this out and thought I would post a working sample since I could not find one that was complete.
Plus I'm going out of my way to not use JQuery anymore, and since this is within an Angular V6 component why not try it in TypeScript.

So the concept is to either select a image file using the file input or drop a image file on the target and show the image selected or dropped. Then use the FileReader and extract the Base64 and assign it to the src of the image element target, or you can create a new image element if you choose and assign the new src to it.

Point being to show the user something has happened to satisfy their new for instant gratification and to solve the issue of not wasting time going to the server, doing lots or work and sending it back.

In TypeScript, reader.result can return either a string or ArrayBuffer described as string|ArrayBuffer which was confusing to me because I had to make the TypeScript Lint happy so it would compile.

This would not work and the lint complained that type ArrayBuffer could not be converted to string.
b64Image = reader.result;<br />
dI.setAttribute('src', b64Image);

This would not work either with the same complaint
b64Image = reader.result;<br />
dI.setAttribute('src', b64Image.toString());

This worked without complaint
b64Image = reader.result.toString();<br />
dI.setAttribute('src', b64Image);


Working sample in TypeScript for an Angular 6 component.
9/19/18 16:43 - I got this wrong when I posted it. Had to research it again and add the listener.
Works but really slow setting the b64 hidden text box.
onDrop(event: any) {

    // Grab the files selected being dragged over
    let dI                  = document.getElementById('dropImage'),
        b64                 = document.getElementById('AvatarB64'),
        files               = event.target.items || event.dataTransfer.items,
        file                = files[0].getAsFile(),
        reader              = new FileReader,
        b64Image            = '';

    this.containerReset();
    this.imageFilename = file.name;
    event.dataTransfer.dropEffect = 'copy';

    // Get the image blob from the file selected.
    if (file) {

        this.imageType = file.type;
        reader.addEventListener('load', function () {
            b64Image = reader.result.toString();
            dI.setAttribute('src', b64Image);
            b64.setAttribute('value', b64Image);
        }, false);

        reader.readAsDataURL(file);

    };
    reader.onerror = (error) => {
        console.log('', error);
    };

    event.preventDefault();
    event.stopPropagation();
}
One thing here is the use of arrows, something I need to read up on. I suppose I could try it and see what happens but I need to move on and write the File Button code.
reader.onloadend = function () {
reader.onloadend = () => {

If you have any suggestions feel free to comment on it. Remember this is TypeScript.
Perhaps one day CP will add discussions for TypeScript or really Angular, React and NodeJS. I'm pretty excited about it and will most likely make the move to these technologies.
If it ain't broke don't fix it
Discover my world at jkirkerx.com


modified 19-Sep-18 21:45pm.

QuestionCan't match this string in typescript using if and else if Pin
jkirkerx18-Sep-18 13:04
professionaljkirkerx18-Sep-18 13:04 
AnswerRe: Can't match this string in typescript using if and else if Pin
Graham Breach18-Sep-18 21:21
Graham Breach18-Sep-18 21:21 
GeneralRe: Can't match this string in typescript using if and else if Pin
jkirkerx19-Sep-18 7:27
professionaljkirkerx19-Sep-18 7:27 
QuestionCan't match this string in typescript using if and else if Pin
jkirkerx18-Sep-18 13:04
professionaljkirkerx18-Sep-18 13:04 
QuestionHow to cancel a drag event Pin
jkirkerx18-Sep-18 9:25
professionaljkirkerx18-Sep-18 9:25 
AnswerRe: How to cancel a drag event Pin
Richard Deeming18-Sep-18 10:53
mveRichard Deeming18-Sep-18 10:53 
GeneralRe: How to cancel a drag event Pin
jkirkerx18-Sep-18 11:54
professionaljkirkerx18-Sep-18 11:54 
Questioncouldn't read from datalist Pin
thepast16-Sep-18 21:01
thepast16-Sep-18 21:01 
QuestionHow to pass JavaScript output to an HTML String Variable outside the <script>? Pin
Member 1348136115-Sep-18 15:45
Member 1348136115-Sep-18 15:45 
AnswerRe: How to pass JavaScript output to an HTML String Variable outside the <script>? Pin
Richard Deeming18-Sep-18 3:45
mveRichard Deeming18-Sep-18 3:45 
QuestionTrouble uploading files with aws s3 Pin
Member 1398382813-Sep-18 13:15
Member 1398382813-Sep-18 13:15 
AnswerRe: Trouble uploading files with aws s3 Pin
jkirkerx1-Oct-18 8:04
professionaljkirkerx1-Oct-18 8:04 
Questiongetting error period.draggable is not a function Pin
Gopal Kan12-Sep-18 19:49
Gopal Kan12-Sep-18 19:49 
AnswerRe: getting error period.draggable is not a function Pin
Graham Breach12-Sep-18 21:08
Graham Breach12-Sep-18 21:08 
QuestionModal popup with datalist Pin
thepast12-Sep-18 8:18
thepast12-Sep-18 8:18 
QuestionJavascript and extensions Pin
Member 1398114211-Sep-18 15:10
Member 1398114211-Sep-18 15:10 
QuestionWhen pressing right and left keys object not moving in javascript game. Pin
Member 1396097131-Aug-18 11:10
Member 1396097131-Aug-18 11:10 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.