3

I want to upload and download the files using javascript.

Step 1: I converted the uploaded files to binary code and successfully able to send the binary code to server through web api.

       var SA_Supp_Evidence_Object_File = null;
        var Evidence_file_name = null;

        if (document.getElementById("file").files.length != 0) {
            SA_Supp_Evidence_Object_File = document.getElementById('file').files[0];

            var fr = new FileReader;
            fr.onloadend = function () {
                alert(fr.result);
            };
            fr.readAsBinaryString(SA_Supp_Evidence_Object_File);

            Evidence_file_name = SA_Supp_Evidence_Object_File.name;
        };

Step 2: Now i want to retrieve the same data from database and able to download by clicking the link

<td><a href="_self">Download</a></td>

Can any one help me to solve this problem

I tried like this

var data = 'data:text/plain;base64,'+L_EncodedData;
document.location = data;

1 Answer 1

2

You need to change the href attribute of the <a> to contain the data you want the user to download. You don't want to do a redirect via document.location, instead the anchor itself should contain the data.

e.g.:

var a = document.body.appendChild(
    document.createElement("a")
);
a.download = "filename.dat";
a.href = "data:text/plain;base64," + L_EncodedData;
a.innerHTML = "download";
Sign up to request clarification or add additional context in comments.

2 Comments

<a href="data:image/jpeg;base64,skjdfbsdhfbsmhdfbsmdhfvbsdvfnsdvfnsdfvsndvfnsdvfnsdvbfbnsdvbfnsd sdmfsdvbnbfbvsdnbfvsndbfvnsdbvfnsdvfnsdbfnsdbvfsndbvfnsdbvfsnd smdmfvsmndbvfnsbdvfsdnbfvsndbfvsdbnfvsbdfvsdb" download="filename.png">sdfsdfsdfs</a> Not working in IE
IE doesn't fully support inline data yet. en.wikipedia.org/wiki/Data_URI_scheme

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.