Replace type file by fixed url variable

HTML:

<a onclick="call_menu(FILE, 'file_open');" href="#">Choose file...</a>

Javascript:

 //open
    this.file_open = function () {
        this.open();
    };


this.open = function () {
    document.getElementById("tmp").innerHTML = '';
    var a = document.createElement('input');
    a.setAttribute("id", "file_open");
    a.type = 'file';
    a.multiple = 'multiple ';
    document.getElementById("tmp").appendChild(a);
    document.getElementById('file_open').addEventListener('change', this.open_handler, false);


    //force click
    document.querySelector('#file_open').click();
};

In html to click “Choose file” runs the excerpt above javascript.

But now I need to be different, I must eliminate the option to choose file and put a variable that will receive a file (url) set / fixed.

“this.open_handler” is the function below:

this.open_handler = function (e) {
    var files = e.target.files;
    for (var i = 0, f; i < files.length; i++) {
        f = files[i];
        if (!f.type.match('image.*') && f.type != 'text/xml')
            continue;
        if (files.length == 1)
            this.SAVE_NAME = f.name.split('.')[f.name.split('.').length - 2];


        var FR = new FileReader();
        FR.file = e.target.files[i];


        FR.onload = function (event) {
            if (this.file.type != 'text/xml') {
                //image
                LAYER.layer_add(this.file.name, event.target.result, this.file.type);
                EXIF.getData(this.file, this.save_EXIF);
            }
            else {
                //xml
                var responce = FILE.load_xml(event.target.result);
                if (responce === true)
                    return false;
            }
        };
        if (f.type == "text/plain")
            FR.readAsText(f);
        else if (f.type == "text/xml")
            FR.readAsText(f);
        else
            FR.readAsDataURL(f);
    }};

My goal is just to remove the front end button “choose file” and create a variable with a fixed value (containing the url of the file)

I thought the following:

I exclude the two functions: “this.open” and “this.file_open”, then in the “this.open_handler” function, delete the loop for, but I do not know how to properly take the loop and insert the variable with the url fixed:

var urlmyFixedImage = "image.png";

I am a newbie with javascript

That code you’ve shown is working with a file someone has selected on their computer, are you wanting to change it to download a file from a server and then process?

It’s not possible to access images from a users computer in JavaScript without the file input and having them select it.

Thanks for your answer.

Currently the code open a file through the button to the user to choose the file.

Now I’m trying to put a still image that the server (myaplication.com/images/myimage.png). So when the user open the application image is already loaded

var urlmyFixedImage = "myaplication.com/images/myimage.png";

Forgive me for asking but is there a reason you can’t use <img src="myaplication.com/images/myimage.png"> for this?

I’m sorry I have not explained before.
The reason is that the JavaScript code is part of an image editor.
It is an application to paint the image

Ah right, perhaps this is what you’re after?

I think the best mechanisms you’ll find is to load an <img> or Image in javascript and to access whatever properties it exposes.

See this is the editor I’m using;

I simply want to eliminate the option “open…”, and load the application already has an open image to paint

Complete code javascript for " file" relating to open and save file:

https://github.com/viliusle/miniPaint/blob/master/js/file.js

I may be off the mark here, but this sounds a lot like what you are trying to do.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.